私はtkinterにOptionMenuを持っています。メニューのオプションは、辞書のキーです。各キーの値は4つの項目を含むリストです。tkinter OptionMenuの辞書値(辞書の値はリスト)にアクセスし、変数に格納
選択したメニューオプションを使用して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()
は、問題の簡単な作業コードを表示 - 誰もがそれをテストし、あなたのための一例を作ることができます。 – furas
OK。コードが追加されました。 – Paulo19