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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:35:33  来源:igfitidea点击:

Changing the options of a OptionMenu when clicking a Button

pythonpython-2.7tkinteronclickoptionmenu

提问by charmoniumQ

Say I have an option menu network_selectthat 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 .configbecause I looked through network_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.Menuwidget:

相同,但使用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 对象上有一个方便的方法。