问题描述
我的 GUI 中有一个由列表填充的 OptionMenu.每次用户运行某个进程时,列表都会更新以反映这一点.有没有办法根据列表更新OptionMenu?我已经按照 尝试了 self.plotWindow.update()
这个问题但无济于事.但是,关闭并重新打开窗口确实会像您期望的那样刷新 OptionMenu.相关代码:
I have an OptionMenu in my GUI that is being populated by a list. Every time the user runs a certain process the list updates to reflect this. Is there a way to update the OptionMenu based on the list? I've tried self.plotWindow.update()
as per this question but to no avail. However, closing and reopening the window does refresh the OptionMenu as you would expect. Relevant code:
if self.figNum.get() == 'New Figure...':
if self.figList[-1] == 'New Figure...':
self.figList.append(1)
else:
self.figList.append(self.figList[-1]+1)
self.plotWindow.update() #tk.Tk() window
self.i = self.figList[-1]
else:
self.i = self.figNum.get()
推荐答案
OptionMenu 中的选项不绑定到创建它们的列表.因此,更改列表不会更改 OptionMenu,您必须自己更新它.
The options in an OptionMenu are not bound to the list from which they are created. Therefore, changing the list does not change the OptionMenu, you'll have to update it yourself.
您可以通过获取 OptionMenu 的 menu
来实现,并向其添加命令.以下示例展示了如何执行此操作(基于 此答案).
You can do that by getting the OptionMenu's menu
, and add commands to that. The following example shows how to do this (based on this answer).
它表明,即使 self.options
列表附加了一个使用向列表添加选项"按钮的选项,OptionMenu 不会自动更改.要更新 OptionMenu,您可以为此使用更新选项菜单"按钮,该按钮调用 self.update_option_menu
.此函数从 OptionMenu 中删除所有选项,并为 self.options
中的每个项目插入一个新选项.
It shows that, even though the self.options
list is appended with an option using the 'Add option to list' button, the OptionMenu does not change automatically. To update the OptionMenu, you can use the 'Update option menu' button for this, which calls self.update_option_menu
. This function deletes all options from the OptionMenu, and inserts a new one for each item in self.options
.
import Tkinter as tk
class App():
def __init__(self, parent):
self.parent = parent
self.options = ['one', 'two', 'three']
self.om_variable = tk.StringVar(self.parent)
self.om_variable.set(self.options[0])
self.om_variable.trace('w', self.option_select)
self.om = tk.OptionMenu(self.parent, self.om_variable, *self.options)
self.om.grid(column=0, row=0)
self.label = tk.Label(self.parent, text='Enter new option')
self.entry = tk.Entry(self.parent)
self.button = tk.Button(self.parent, text='Add option to list', command=self.add_option)
self.label.grid(column=1, row=0)
self.entry.grid(column=1, row=1)
self.button.grid(column=1, row=2)
self.update_button = tk.Button(self.parent, text='Update option menu', command=self.update_option_menu)
self.update_button.grid(column=0, row=2)
def update_option_menu(self):
menu = self.om["menu"]
menu.delete(0, "end")
for string in self.options:
menu.add_command(label=string,
command=lambda value=string: self.om_variable.set(value))
def add_option(self):
self.options.append(self.entry.get())
self.entry.delete(0, 'end')
print self.options
def option_select(self, *args):
print self.om_variable.get()
root = tk.Tk()
App(root)
root.mainloop()
这篇关于从列表更新 OptionMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!