将2个组合框链接到单个函数并返回其值

时间:2015-02-05 15:25:23

标签: python combobox tkinter ttk

我有两个组合框,我想用一个函数来控制,但我很难理解如何从任何组合框调用回调中获取值。

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 个答案:

答案 0 :(得分:1)

调用绑定函数时自动传递的事件对象包含属性widget,该属性是触发事件的窗口小部件。所以你可以绑定两个组合框&#39; <<ComboboxSelected>>触发self.information(没有括号)并将其定义为

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