如何返回在 tkinter 中作为命令给出的函数的值 [英] How to return a value of a function given as a command in tkinter

查看:75
本文介绍了如何返回在 tkinter 中作为命令给出的函数的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 python tkinter 编写了一个非常简单的代码,它包含一个输入框.我想将用户插入的值保留给我自己,以备以后需要使用.

I've written a very simple code with python tkinter, it contains an input box. I'd like to keep the value inserted by the user to myself, in case I need to use it later.

代码如下:

import tkinter as tk 
   
root=tk.Tk() 
  
root.geometry("600x400") 
   
def submit(): 
  
    name=name_entry.get() 
    return name
       
name_label = tk.Label(root, text = 'Username', 
                      font=('calibre', 
                            10, 'bold')) 
    
name_entry = tk.Entry(root, 
                      font=('calibre',10,'normal')) 
     
sub_btn=tk.Button(root,text = 'Submit', 
                  command = submit) 

name_label.grid(row=0,column=0) 
name_entry.grid(row=0,column=1)  
sub_btn.grid(row=2,column=1) 

root.mainloop() 

在函数submit中我写了return name,为了返回插入的用户名.但是如何在函数之外访问它呢?我想将值保留在某处,但我不知道如何保存.

In function submit I've written return name, in order to return the inserted name of the user. But how can I access it outside of the function? I want to keep the value somewhere, but I don't know how to.

我提前感谢您的帮助

推荐答案

大多数时候,回调会丢弃返回值(所有 tkinter 回调都会这样做),如果有的话.这是因为使用最常用于回调的观察者模式来管理返回值有点尴尬.

Most times, callbacks drop the return value (all tkinter callbacks do), if any. This is because is is a bit awkward to manage return values using the observer pattern which is most often used for callbacks.

有两种主要方法可以将值从 CB 转移到需要的地方:

There are two main ways to transfer a value from the CB to where it is needed:

一个是使用一个容器对象——它可能是一个简单的pythonlistdict,或者一个更复杂的datacallback 可访问的类,并对其进行变异以为其分配适当的值.

One is to use a container object - that may be a simple python list or dict, or a more elaborate data class that is accessible by the callback, and mutated to assign the proper values to it.

这是一个使用字典的非常简单的例子:

here is a very simple example using a dictionary:

import tkinter as tk 

def print_info():
    print(f'from print_info: {information_transfer}')

def submit(): 
    information_transfer['name'] = name_entry.get() 
    print(f'after input: {information_transfer}')
       
root=tk.Tk()   
root.geometry("600x400") 
   
name_label = tk.Label(root, text = 'Username', font=('calibre', 10, 'bold')) 
name_entry = tk.Entry(root, font=('calibre', 10, 'normal')) 
     
sub_btn = tk.Button(root,text='Submit', command=submit)
info_btn = tk.Button(root,text='print info', command=print_info)


name_label.grid(row=0, column=0) 
name_entry.grid(row=0, column=1)  
sub_btn.grid(row=2, column=1) 
info_btn.grid(row=3, column=1) 

information_transfer = {'name': None, 'other': None}
print(f'before input: {information_transfer}')

root.mainloop()

另一个是@DavidGildour 在另一个答案中描述的 OOP 方法.

Another is the OOP approach described by @DavidGildour in another answer.

这篇关于如何返回在 tkinter 中作为命令给出的函数的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆