pythontkintercombobox

Python Tkinter - How do I read the value of a selected ComboBox within classed frames?


I have put a ComboBox in my code, which works. However, I am trying to find a way to read the value of the ComboBox when the user selects an option.

I have also used classes to define each of my frames that appear, that way I can transition between windows.

I am pretty inexperienced with Tkinter and cannot wrap my head around the ComboBox.

Here is my code for the part where I need to use a ComboBox:


class EntryPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 1", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()

        def gunvarselect():
            gunselected = (gunvar)
            print(gunselected)

        gunvar = tk.StringVar()
        gunselect =ttk.Combobox(self, textvariable=gunvar)
        gunselect.bind(self, '<<ComboboxSelected>>', gunvarselect)
        gunselect['values'] = ('Pistols', 'Shotguns', 'Rifles', 'Sniper Rifles', 'Sub-machine Guns', 'Light-machine Guns')
        gunvarselect()
        gunselect.pack(side="left", expand="false", pady=20, padx=50)








        gunvar2 = tk.StringVar()
        gunselect2 = ttk.Combobox(self, textvariable=gunvar2)
        gunselect2['values'] = (
        'Pistols', 'Shotguns', 'Rifles', 'Sniper Rifles', 'Sub-machine Guns', 'Light-machine Guns')
        gunselect2.pack(side="right", expand="false", pady=20, padx=50)

I have tried to read the value through linking an event, however, I could not end up configuring it properly.

I also tried to read the value through just the textvariable, but I don't think that would be efficient.


Solution

    1. Define the gunvarselect function to take an event parameter:

      • This function retrieves the selected value from the gunvar StringVar and prints it.
    2. Fixed the bind method call:

      • Change the incorrect self to the correct gunselect object, and correctly bound the <<ComboboxSelected>> event to the gunvarselect function.

    Here is the simplified and corrected portion of your code:

    class EntryPage(tk.Frame):
    
        def __init__(self, parent, controller):
            tk.Frame.__init__(self, parent)
            self.controller = controller
            label = tk.Label(self, text="This is page 1", font=controller.title_font)
            label.pack(side="top", fill="x", pady=10)
            button = tk.Button(self, text="Go to the start page",
                               command=lambda: controller.show_frame("StartPage"))
            button.pack()
    
            def gunvarselect(event):
                gunselected = gunvar.get()
                print(gunselected)
    
            gunvar = tk.StringVar()
            gunselect = ttk.Combobox(self, textvariable=gunvar)
            gunselect['values'] = ('Pistols', 'Shotguns', 'Rifles', 'Sniper Rifles', 'Sub-machine Guns', 'Light-machine Guns')
            gunselect.bind('<<ComboboxSelected>>', gunvarselect)
            gunselect.pack(side="left", expand="false", pady=20, padx=50)
    
            gunvar2 = tk.StringVar()
            gunselect2 = ttk.Combobox(self, textvariable=gunvar2)
            gunselect2['values'] = (
            'Pistols', 'Shotguns', 'Rifles', 'Sniper Rifles', 'Sub-machine Guns', 'Light-machine Guns')
            gunselect2.pack(side="right", expand="false", pady=20, padx=50)