Python 单击按钮时更改 OptionMenu 的选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17580218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing the options of a OptionMenu when clicking a Button
提问by charmoniumQ
Say I have an option menu network_select
that has a list of networks to connect to.
假设我有一个选项菜单network_select
,其中包含要连接的网络列表。
import Tkinter as tk
choices = ('network one', 'network two', 'network three')
var = tk.StringVar(root)
network_select = tk.OptionMenu(root, var, *choices)
Now, when the user presses the refresh button, I want to update the list of networks that the user can connect to.
现在,当用户按下刷新按钮时,我想更新用户可以连接到的网络列表。
- I don't I can use
.config
because I looked throughnetwork_select.config()
and didn't see an entry that looked like the choices I gave it. - I don't think this is something one can change using a tk variable, because there is no such thing as a
ListVar
.
- 我不知道我可以使用,
.config
因为我浏览network_select.config()
并没有看到一个看起来像我给它的选择的条目。 - 我不认为这是可以使用 tk 变量改变的东西,因为没有
ListVar
.
采纳答案by charmoniumQ
I modified your script to demonstrate how to do this:
我修改了您的脚本以演示如何执行此操作:
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()
As soon as you click the "Refresh" button, the options in network_select are cleared and the ones in new_choices are inserted.
单击“刷新”按钮后,network_select 中的选项将被清除,而 new_choices 中的选项将被插入。
回答by FooBar167
The same, but with tk.Menu
widget:
相同,但使用tk.Menu
小部件:
# Using lambda keyword and refresh function to create a dynamic menu.
import tkinter as tk
def show(x):
""" Show menu items """
var.set(x)
def refresh(l):
""" Refresh menu contents """
var.set('')
menu.delete(0, 'end')
for i in l:
menu.add_command(label=i, command=lambda x=i: show(x))
root = tk.Tk()
menubar = tk.Menu(root)
root.configure(menu=menubar)
menu = tk.Menu(menubar, tearoff=False)
menubar.add_cascade(label='Choice', menu=menu)
var = tk.StringVar()
l = ['one', 'two', 'three']
refresh(l)
l = ['four', 'five', 'six', 'seven']
tk.Button(root, text='Refresh', command=lambda: refresh(l)).pack()
tk.Label(root, textvariable=var).pack()
root.mainloop()
回答by Ingvar Lond
In case of using ttk, there is a convenient set_menu(default=None, values)
method on the OptionMenu object.
在使用 ttk 的情况下,set_menu(default=None, values)
OptionMenu 对象上有一个方便的方法。