我在tkinter中有一个OptionMenu。菜单中的选项是字典中的键。每个键的值是一个包含4个项目的列表。
如何使用所选菜单选项将4个项目分配给单独的变量?
from tkinter import *
root = Tk()
options = {'option 1' : ['list item 1' , 'list item 2' , 'list item 3' , 'list item 4'] , 'option 2' : ['list item w' , 'list item x' , 'list item y' , 'list item z']}
options = sorted(options)
var = StringVar(root)
var.set('Choose an option')
option = OptionMenu(root, var, *options)
option.pack()
selection = StringVar()
def changeOption(*args):
newSelection = options[var.get()]
selection.set(newSelection)
var.trace('w', changeOption)
variable1 = # if option 1 was selected from the menu then this variable would contain list item 1
variable2 = # if option 1 was selected from the menu then this variable would contain list item 2
variable3 = # if option 1 was selected from the menu then this variable would contain list item 3
variable4 = # if option 1 was selected from the menu then this variable would contain list item 4
root.mainloop()
答案 0 :(得分:2)
您必须在功能change_option
中执行此操作,而不是在主要部分中执行此操作。
主要部分仅创建窗口/ GUI并启动mainloop()
。然后mainloop()
控制所有内容 - 当您更改change_option
中的选项时,它会执行函数OptionMenu
。
您可以使用var.get()
或command=
发送的第一个参数获取密钥,然后您可以从字典中获取数据。
但您无法将sorted()
分配给options
,因为sorted()
仅返回已排序键的列表,而您无法访问oryginal字典。
keys = sorted(options)
完整代码:
from tkinter import *
# --- functions ---
def change_option(*args):
# selected element
print(' args:', args)
print('var.get():', var.get())
# get list from dictionary `options`
data = options[var.get()]
data = options[args[0]]
print(' data:', data[0], data[1], data[2], data[3])
# if you really need in separated varaibles
variable1 = data[0]
variable2 = data[1]
variable3 = data[2]
variable4 = data[3]
print('variables:', variable1, variable2, variable3, variable4)
print('---')
# --- main ---
root = Tk()
options = {
'option 1': ['list item 1', 'list item 2', 'list item 3', 'list item 4'],
'option 2': ['list item w', 'list item x', 'list item y', 'list item z']
}
keys = sorted(options) # don't overwrite `options` - sorted() returns only keys from dictionary.
var = StringVar(root)
var.set('Choose an option')
option = OptionMenu(root, var, *keys, command=change_option)
option.pack()
root.mainloop()
结果:
args: ('option 1',)
var.get(): option 1
data: list item 1 list item 2 list item 3 list item 4
variables: list item 1 list item 2 list item 3 list item 4
---
args: ('option 2',)
var.get(): option 2
data: list item w list item x list item y list item z
variables: list item w list item x list item y list item z
---
答案 1 :(得分:1)
您可以使用OptionMenu的command
选项。每次从下拉列表中选择一个选项时,该命令都会执行。
from tkinter import *
root = Tk()
def change_vars(e):
for i in range(len(options[var.get()])):
vars[i].set(options[var.get()][i])
#these two prints added for debugging purposes
#to see if we are getting and setting right values
print(options[var.get()])
for item in vars:
print(item.get())
options = {'option 1':['list item 1','list item 2','list item 3','list item 4'] , 'option 2':['list item w','list item x','list item y','list item z']}
var = StringVar(root)
var.set('Choose an option')
option = OptionMenu(root, var, *options, command=change_vars)
option.pack()
vars = [StringVar() for _ in range(len(options[0]))] #creates a list of 4 stringvars
root.mainloop()
在这里,我不是硬编码所有变量,而是在循环中创建它们并将它们存储在列表中。