在 tkinter OptionMenu 中访问字典值(字典值是一个列表)并存储在变量中 [英] Access a dictionary value (dictionary value is a list) in tkinter OptionMenu and store in variable

查看:28
本文介绍了在 tkinter OptionMenu 中访问字典值(字典值是一个列表)并存储在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 tkinter 中有一个 OptionMenu.菜单中的选项是字典中的键.每个键的值是一个包含 4 个项目的列表.

I have an OptionMenu in tkinter. The options in the menu are keys from a dictionary. The value of each key is a list that contains 4 items.

如何使用选定的菜单选项将 4 个项目分配给单独的变量?

How do I use the selected menu option to assign the 4 items to separate variables?

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

推荐答案

你必须在函数 change_option 中完成,而不是在 main 部分.

You have to do it in function change_option, not in main part.

主要部分只创建windows/GUI并启动mainloop().然后 mainloop() 控制一切 - 当您更改 OptionMenu 中的选项时,它会执行函数 change_option.

Main part only create windows/GUI and starts mainloop(). And then mainloop() controls everything - it executes function change_option when you change option in OptionMenu.

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

You can use your var.get() or first argument send by command= to get key and then you can get data from dictionary.

但是您不能将 sorted() 分配给 options 因为 sorted() 只返回已排序的键列表,而您无法访问原始字典.

But you can't assign sorted() to options because sorted() returns only list of sorted keys and you loose access to oryginal dictionary.

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

这篇关于在 tkinter OptionMenu 中访问字典值(字典值是一个列表)并存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆