单击按钮时更改OptionMenu的选项
发布于 2021-01-29 19:10:19
说我有一个选项菜单network_select
,其中包含要连接的网络列表。
import Tkinter as tk
choices = ('network one', 'network two', 'network three')
var = tk.StringVar(root)
network_select = tk.OptionMenu(root, var, *choices)
现在,当用户按下刷新按钮时,我想更新用户可以连接的网络列表。
- 我无法使用,
.config
因为我浏览了所有内容network_select.config()
,但没有看到看起来像我所做选择的条目。 - 我不认为这可以使用tk变量来更改,因为没有这样的事情
ListVar
。
关注者
0
被浏览
49
1 个回答
-
我修改了脚本以演示如何执行此操作:
import Tkinter as tk root = tk.Tk() choices = ('network one', 'network two', 'network three') var = tk.StringVar(root) def refresh(): # Reset var and delete all old options var.set('') network_select['menu'].delete(0, 'end') # Insert list of new options (tk._setit hooks them up to var) new_choices = ('one', 'two', 'three') for choice in new_choices: network_select['menu'].add_command(label=choice, command=tk._setit(var, choice)) network_select = tk.OptionMenu(root, var, *choices) network_select.grid() # I made this quick refresh button to demonstrate tk.Button(root, text='Refresh', command=refresh).grid() root.mainloop()
单击“刷新”按钮后,将清除network_select中的选项,并插入new_choices中的选项。