Retrieve choosen value of a ttk.combobox Retrieve choosen value of a ttk.combobox tkinter tkinter

Retrieve choosen value of a ttk.combobox


You do not need a bind to track the variable. You can do this with a StringVar.That said you cannot just call Port = PortCOM.get in the global as the code is initiated and expect to get anything. 1st issue with that the correct syntax is Port = PortCOM.get() with parenthesis. 2nd you are calling get() at init and thus the only possible value would be an empty string if not an error.

The next issue I see is the bind() this is not doing what you think it is doing. The bind is used to call a function not to update a variable directly.

The correct use of Combobox is to use a textvariable with an IntVar() or StringVar() depending on the values and then use get() on that var when you need it in a function.

from tkinter import *import tkinter.ttk as ttkroot = Tk()textVar = StringVar(root)textVar.set('')PortCOM = ttk.Combobox(root, textvariable=textVar, values=[1, 2, 3, 4, 5, 6])PortCOM.pack()def print_value():    print(textVar.get())Button(root, text='Print Value', command=print_value).pack()root.mainloop()

If you really want to use bind() for some reason like to have the selection immediately do something when selected then try this instead.

Make sure the bind call is after the function used to do something with your combobox.

from tkinter import *import tkinter.ttk as ttkroot = Tk()textVar = StringVar(root)textVar.set('')PortCOM = ttk.Combobox(root, textvariable=textVar, values=[1, 2, 3, 4, 5, 6])PortCOM.pack()# Note the _ in the argument section of the function.# A bind will send an event to the function selected unless you use a lambda.# so to deal with events we don't care about we have a few options.# We can use an underscore to just except any argument and do nothing with it.# We could also do event=None but typically I only use that when a function might use the event variable but not always.def print_value(_):     print(textVar.get())    print(PortCOM.get())PortCOM.bind("<<ComboboxSelected>>", print_value)root.mainloop()


And if you don't want extra button you can just do this

from tkinter import *import tkinter.ttk as ttkPort=''root = Tk()def set_port(_):    global Port    Port = PortCOM.get()    print(Port)PortCOM = ttk.Combobox(root, values=[1,2,3,4,5,6])PortCOM.bind("<<ComboboxSelected>>", set_port)PortCOM.pack ()root.mainloop()


Look at this OO example to use all the power of the combo ;)

#!/usr/bin/python3import tkinter as tkfrom tkinter import ttkfrom tkinter import messageboxclass Main(ttk.Frame):    def __init__(self, parent):        super().__init__()        self.parent = parent        self.values = ('Apple','Banana','Orange','Grapes','Watermelon','Plum','Strawberries','Pear')        self.init_ui()    def init_ui(self):        self.pack(fill=tk.BOTH, expand=1)        f = ttk.Frame()        ttk.Label(f, text = "Combobox").pack()        self.cbCombo = ttk.Combobox(f,state='readonly',values=self.values)        self.cbCombo.pack()        w = ttk.Frame()        ttk.Button(w, text="Callback", command=self.on_callback).pack()        ttk.Button(w, text="Reset", command=self.on_reset).pack()        ttk.Button(w, text="Set", command=self.on_set).pack()        ttk.Button(w, text="Close", command=self.on_close).pack()        f.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)        w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=1)    def on_callback(self,):        if self.cbCombo.current() != -1:            msg = "You have selected:\n{0}".format(self.cbCombo.get())        else:            msg = "You did not select anything"        messagebox.showinfo(self.parent.title(), msg)    def on_reset(self):        self.cbCombo.set('')    def on_set(self):        self.cbCombo.current(0)    def on_close(self):        self.parent.on_exit()class App(tk.Tk):    """Start here"""    def __init__(self):        super().__init__()        self.protocol("WM_DELETE_WINDOW", self.on_exit)        self.set_title()        self.set_style()        frame = Main(self,)        frame.pack(fill=tk.BOTH, expand=1)    def set_style(self):        self.style = ttk.Style()        #('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative')        self.style.theme_use("clam")    def set_title(self):        s = "{0}".format('Simple App')        self.title(s)    def on_exit(self):        """Close all"""        if messagebox.askokcancel("Simple App", "Do you want to quit?", parent=self):            self.destroy()               if __name__ == '__main__':    app = App()    app.mainloop()

enter image description here