2017-08-04 57 views
0

我想从下面的代码中的第二个下拉选项中读取所选下拉列表的值。 [示例:“德国”,“法国”,“瑞士”]Python - tkinter如何读取第二个下拉值

在这里我可以能够通过使用函数调用的乐趣()

但同样是无法读取第二下降到读取第一个下拉值向下值。

建议我如何从下面的代码

读第二个下拉值以下是代码。

import sys 
if sys.version_info[0] >= 3: 
    import tkinter as tk 
else: 
    import Tkinter as tk 


class App(tk.Frame): 

    def __init__(self, master): 
     tk.Frame.__init__(self, master) 

     self.dict = {'Asia': ['Japan', 'China', 'Malaysia'], 
        'Europe': ['Germany', 'France', 'Switzerland']} 

     self.variable_a = tk.StringVar(self) 
     self.variable_b = tk.StringVar(self) 

     self.variable_a.trace('w', self.update_options) 

     self.optionmenu_a = tk.OptionMenu(self, self.variable_a, *self.dict.keys(), command=self.fun) 
     self.optionmenu_b = tk.OptionMenu(self, self.variable_b, '') 

     self.variable_a.set('Asia') 

     self.optionmenu_a.pack() 
     self.optionmenu_b.pack() 
     self.pack() 

    def fun(self,value): 
     print(value) 


    def update_options(self, *args): 
     countries = self.dict[self.variable_a.get()] 
     self.variable_b.set(countries[0]) 

     menu = self.optionmenu_b['menu'] 
     menu.delete(0, 'end') 

     for country in countries: 
      menu.add_command(label=country, command=lambda nation=country: self.variable_b.set(nation)) 


if __name__ == "__main__": 
    root = tk.Tk() 
    app = App(root) 
    app.mainloop() 

回答

0

甲Tkinter的变量有一个trace方法可用于当所述变量被设置为触发的回调函数。为迅速反应

def fun2(self, *args): 
    print(self.variable_b.get()) 
+0

感谢.... 它的工作就像一个魅力....:你的情况,将它添加到__init__

self.variable_b.trace('w', self.fun2) 

,并作出新的方法来处理它! – Shanky

相关问题