2016-12-08 17 views
0

私は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() 
+0

は、問題の簡単な作業コードを表示 - 誰もがそれをテストし、あなたのための一例を作ることができます。 – furas

+0

OK。コードが追加されました。 – Paulo19

答えて

2

本体ではなく機能change_optionで行う必要があります。

メイン部分のみWindows/GUIを作成し、mainloop()を開始します。そしてmainloop()はすべてを制御します。OptionMenuのオプションを変更すると、関数change_optionが実行されます。

var.get()または最初の引数をcommand=で送信してキーを取得し、辞書からデータを取得できます。

sorted()optionsに割り当てることはできません。sorted()はソートされたキーのリストのみを返すため、オリジンディクショナリにアクセスしにくいためです。

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

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

ここでは、すべての変数をハードコーディングする代わりに、ループで作成してリストに格納しました。

関連する問題