Link 2 个组合框到一个函数和 return 它的值

Link 2 comboboxes to a single function and return its value

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

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

这段代码是我最简单的形式,它是信息函数,需要一些额外的细节。

调用绑定函数时自动传递的事件对象包含属性widget,即触发事件的widget。因此,您可以绑定两个组合框的 <<ComboboxSelected>> 以触发 self.information(不带括号)并将其定义为

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