Python 3.x 在tkinter选项菜单中访问字典值(字典值是一个列表),并存储在变量中

Python 3.x 在tkinter选项菜单中访问字典值(字典值是一个列表),并存储在变量中,python-3.x,tkinter,Python 3.x,Tkinter,我在tkinter有一个选项菜单。菜单中的选项是字典中的键。每个键的值是一个包含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

我在tkinter有一个选项菜单。菜单中的选项是字典中的键。每个键的值是一个包含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()

您可以使用OptionMenu的
命令
选项。每次从下拉列表中选择选项时,该命令都会执行

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

在这里,我没有硬编码所有变量,而是在一个循环中创建它们并将它们存储在一个列表中。

您可以使用OptionMenu的
命令
选项。每次从下拉列表中选择选项时,该命令都会执行

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

在这里,我没有对所有变量进行硬编码,而是将它们创建在一个循环中并存储在一个列表中。

您必须在函数
change\u option
中执行此操作,而不是在主要部分

主要部分仅创建windows/GUI并启动
mainloop()
。然后
mainloop()
控制一切-当您在
OptionMenu
中更改选项时,它执行函数
change\u option

您可以使用
var.get()
或通过
command=
发送的第一个参数来获取密钥,然后可以从字典中获取数据

keys = sorted(options)
但是您不能将
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
---

您必须在函数
change\u选项中执行此操作,而不是在主体部分中

主要部分仅创建windows/GUI并启动
mainloop()
。然后
mainloop()
控制一切-当您在
OptionMenu
中更改选项时,它执行函数
change\u option

您可以使用
var.get()
或通过
command=
发送的第一个参数来获取密钥,然后可以从字典中获取数据

keys = sorted(options)
但是您不能将
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
---

展示有问题的简单、有效的代码——这样每个人都可以测试它并为您做示例。添加代码。显示有问题的简单的工作代码-这样每个人都可以测试它并为您做示例。好的。代码添加。