2015-02-05 22 views
0

我有兩個組合框我想用一個函數來控制,但我正在努力理解如何從哪個組合框調用回調中獲取值。將2個組合框鏈接到一個函數並返回它的值

from Tkinter import * 
import ttk 

class App(Frame): 
    def __init__(self, parent): 
     self.parent = parent 
     self.value_of_combo = "X" 
     self.initUI() 

    def information(self, type): 
     combo_var = self.type.get() 
     print combo_var 

    def initUI(self): 
     # Label 
     self.configlbl = Label(self.parent, text="Description") 
     self.configlbl.pack(side=LEFT) 

     # Type 
     self.linear_value = StringVar() 
     self.linear = ttk.Combobox(self.parent, textvariable=self.linear_value) 
     self.linear.bind('<<ComboboxSelected>>', self.information('linear')) 
     self.linear.pack(side=LEFT) 
     self.linear['values'] = ('X', 'Y', 'Z') 

     # UTCN 
     self.utcn_value = StringVar() 
     self.utcn = ttk.Combobox(self.parent, textvariable=self.utcn_value) 
     self.utcn.bind('<<ComboboxSelected>>', self.information('utcn')) 
     self.utcn.pack(side=LEFT) 
     self.utcn['values'] = ('A', 'B', 'C') 

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

這段代碼在它最簡單的形式,在這裏我提出來,它是需要一些額外的螺母和螺栓的信息功能。

回答

1

調用綁定函數時自動傳遞的事件對象包含屬性widget,該屬性是觸發事件的小部件。所以,你可以綁定兩個組合框<<ComboboxSelected>>觸發self.information(沒有括號),並定義上,由於隨着

def information(self, event): 
     combo_var = event.widget.get() 
     print combo_var 
+0

現貨! – pickles 2015-02-06 08:42:43

相關問題