【发布时间】:2020-01-03 14:25:46
【问题描述】:
我目前正在尝试使用 tkinter 为一个非常具体的数据处理工具创建 GUI。用户将通过按钮加载文件,并且根据该数据,该工具将显示许多下拉菜单,以选择特定于所提供数据的选项。 现在我在初始化 mainloop 之后一直在创建这些小部件(我需要在事后初始化/更新它们,因为用户还应该能够加载多个文件)。正在显示小部件,但我无法访问它们的值,因为这是函数内部的局部变量。
TLDR:我想在初始化窗口后创建和更新 tkinter 小部件,并且它的值需要可访问
我尝试编写如下所示的函数,该函数将根据其他代码触发。可悲的是,它们提供的值仅反映函数终止时小部件(在本例中为下拉菜单)的值,并且不会随小部件而改变。 将它们更改为全局变量也不能解决更新问题。 事后我研究了创建小部件的不同方法,但似乎没有找到方法。
import tkinter as tki
root = tki.Tk()
root.title('Data Comparison')
#specifying a frame and its grid
frame = tki.Frame(root)
frame.grid(column=9, row=7)
#I'm working with dicts, not lists, because that more accurately represents my
#actual data
numbers= {'one':1, 'two':2, 'three':3, 'four':4}
colors= {'blue':5, 'red':6, 'green':7, 'orange':8, 'pink':9, 'yellow':10}
def createDropdown():
global DDvalue
DDvalue = tki.StringVar(root)
DDvalue.set(list(numbers)[0])
global DDvalues
DDvalues = tki.OptionMenu(frame, DDvalue, *numbers)
DDvalues.grid(column= 7, row = 1)
#the global variables only display the accurate value at the time of creation of the widget
createDropdown()
def displayvalue():
print(str(DDvalue.get()))
DDvalue.trace_add('write', displayvalue())
def updateDropdown():
DDvalues.destroy
btnupdate= tki.Button(frame, text='update', command= updateDropdown)
btnupdate.grid (column= 8, row= 7)
#I also could not delete the widget with the button above
root.mainloop()
现在 trace_add 只打印一次值,在创建小部件时(一个),之后更改值会导致以下错误消息: TypeError: 'NoneType' 对象不可调用
另外,更新按钮无法访问小部件
我想查看在满足某些条件后创建的小部件的值可访问。
【问题讨论】:
-
这个
.trace_add('write', displayvalue())应该写成.trace_add('write', displayvalue),注意没有()和global DDvalue必须是global。跨度> -
嘿,感谢您的评论!当我在全局后删除变量时,我得到一个无效的语法错误。此外,当我删除空括号时,我收到以下错误:TypeError: displayvalue() takes 0 positional arguments but 3 were given
-
你有
#global DDvalue作为comment,我的意思是必须是global DDvalue。 -
我按照描述中的说明进行了尝试。我将编辑代码以包含它。
-
将
def displayvalue():更改为def displayvalue(*args):。