Search code examples
pythontkintercomboboxlabel

Tkinter how to change the text value inside a Label with a ComboBox


I have two widgets, a ComboBox and a Label. The ComboBox has options in it that are supposed to change the text inside of the label when the user selects a new item in the box. I have it printing to the terminal which selection has been made; however I cannot get the value inside of the Label to change. Is this because a Label is static and cannot change? Do I need to use something like a Text box instead of a Label?

Here is my code, I left a lot out because it is a fairly large program and this is a small part...

        def modified(e=None):
            maxx.configure(text=("Max",m))
            print(cbox.get())

        cbox = ttk.Combobox(frame_left, justify=CENTER, state='readonly')
        cbox['values'] = (
            'Steps count',
            'Temp avg',
            'Movement intensity'
            )
        cbox.grid(column=0, row=6, columnspan=1, pady=1, sticky="EW")
        cbox.current(0)

        cbox.bind('<<ComboboxSelected>>', modified)

        m = np.around(max(df[cbox.get()]), 3)
        maxx = Label(frame_left, text=("Max",m))
        maxx.grid(row=7, column=0, padx=0, pady=5, sticky="EW")

How do I change the value inside of the Label when the ComboBox changes?


Solution

  • You need to put the whole code for more context because we don't understand everything. Nevertheless, if you want to keep track of what's inside the combobox you have to assign it a StringVar() like this:

    var_combobox = StringVar()
    cbox = ttk.Combobox(frame_left, justify=CENTER, state='readonly', textvariable=var_combobox)
    

    You can then read whenever you want the value of the combobox with the get method of the StringVar so in your case you want to print what's inside the combobox in the label i think. Moreover, you're modified function makes reference to "maxx" label which is created only after the function is define so you have either to create the function after you create your label for the label to exist or pass the label as an argument of the function along with the argument of the text value. Finally, you have to put the bind after every the label is created else it makes no sense to modify a widget that doesn't even exist. The whole code give :

    from tkinter import *
    from tkinter import ttk
    
    root = Tk()
    frame_left = Frame(root)
    frame_left.pack()
    
    def modified(label, text_value):
        maxx.configure(text=("Max", text_value))
        print(cbox.get())
    
    var_combobox = StringVar()
    cbox = ttk.Combobox(frame_left, justify=CENTER, state='readonly', textvariable=var_combobox)
    cbox['values'] = (
        'Steps count',
        'Temp avg',
        'Movement intensity'
        )
    cbox.grid(column=0, row=6, columnspan=1, pady=1, sticky="EW")
    cbox.current(0)
    
    maxx = Label(frame_left, text=("Max"))
    maxx.grid(row=7, column=0, padx=0, pady=5, sticky="EW")
    
    cbox.bind('<<ComboboxSelected>>', lambda event: modified(maxx, var_combobox.get()))
    
    root.mainloop()