Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 单击按钮时更改选项菜单的选项_Python_Python 2.7_Tkinter_Onclick_Optionmenu - Fatal编程技术网

Python 单击按钮时更改选项菜单的选项

Python 单击按钮时更改选项菜单的选项,python,python-2.7,tkinter,onclick,optionmenu,Python,Python 2.7,Tkinter,Onclick,Optionmenu,假设我有一个选项菜单network\u选择,其中有一个要连接的网络列表 import Tkinter as tk choices = ('network one', 'network two', 'network three') var = tk.StringVar(root) network_select = tk.OptionMenu(root, var, *choices) 现在,当用户按下刷新按钮时,我想更新用户可以连接的网络列表 import Tkinter as tk choi

假设我有一个选项菜单
network\u选择
,其中有一个要连接的网络列表

import Tkinter as tk

choices = ('network one', 'network two', 'network three')
var = tk.StringVar(root)
network_select = tk.OptionMenu(root, var, *choices)
现在,当用户按下刷新按钮时,我想更新用户可以连接的网络列表

import Tkinter as tk

choices = ('network one', 'network two', 'network three')
var = tk.StringVar(root)
network_select = tk.OptionMenu(root, var, *choices)
  • 我不知道我可以使用
    .config
    ,因为我查看了
    network\u select.config()
    ,没有看到与我给出的选项类似的条目
  • 我不认为使用tk变量可以改变这一点,因为没有
    ListVar

我修改了您的脚本以演示如何执行此操作:

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()

单击“刷新”按钮后,网络选择中的选项将被清除,新选择中的选项将被插入。

相同,但使用
tk。菜单
小部件:

# 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()

在使用ttk的情况下,OptionMenu对象上有一个方便的
设置菜单(默认值=None,values)
方法。

将其设为对象变量怎么样?谢谢!是否每个Tkinter小部件都有
.keys()
<代码>帮助(网络_选择[菜单])是我所需要的!