0

I have two comboboxes that I would like to control with a single function, but I'm struggling to understand how to get the values from whichever combobox calls the callback.

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

This code in it's simplest form, where I am up to, and it is the information function which needs a few extra nuts and bolts.

nbro
  • 15,395
  • 32
  • 113
  • 196
pickles
  • 3
  • 3

1 Answers1

1

The event object that is automatically passed when a bound function is called contains the attribute widget, which is the widget that triggered the event. So you can bind both comboboxes' <<ComboboxSelected>> to trigger self.information (without parentheses) and define that as

def information(self, event):
        combo_var = event.widget.get()
        print combo_var
fhdrsdg
  • 10,297
  • 2
  • 41
  • 62