Binding selected values of Combobox to subprogram

Viewed 74

My code:

import tkinter as tk
from tkinter import ttk

win = tk.Tk()

def assign():
    value = number.get()
    
    if value == 'op1':
        label = tk.Label(win, text = number)
        label.grid(column = 1, row = 0)

number = tk.StringVar()
box = ttk.Combobox(win, textvariable = number, state = 'readonly')
box['values'] = ('op1','op2','op3')

box.set('op2')

box.grid(column = 0, row = 0)

box.bind('<<ComboboxSelected>>', assign())


win.mainloop()

Explanation: this is how I think my program works.

box, which is the Combobox, has assigned a textvariable named number that, I suppose, takes the values of box, that is, ('op1', 'op2', 'op3'). box is then binded to a subprogram called assign() which will retrieve the value of number and set it equal to value. If value is equal to 'op1', the subprogram will create and grid a label called label that will display the text 'Hello'. Unfortunately, this doesn't work. In other words, my objective is to, when the user selects one of the values given in box['values'], assign() runs and, If the codition is met, display label.

Output:

output

As you can see, even though I selected 'op1', label isn't displayed next to box.

2 Answers

You will have to remove the () while using bind to avoid function call, then create a label outside functions and then update it from the inside using config, like:

label = tk.Label(win)
def assign(event):
    value = number.get() # Get the value
    
    if value == 'op1':
        label.config(text=number) # Update the text
        label.grid(column = 1, row = 0)

box.bind('<<ComboboxSelected>>', assign) # Remove the ()

This way you don't rewrite the labels and just update the labels with new values.

But I think there is no use of StringVar here, just remove it and use box.get() to get the value from the combobox. So according to me the best practice would be:

import tkinter as tk
from tkinter import ttk

win = tk.Tk()

label = tk.Label(win) # Create this outside of function

def assign(event):
    value = box.get() # Get value from combobox
    
    if value == vals[0]: # Check the selected item with the values tuple
        label.config(text=value) # Update the text of label
        label.grid(column=1,row=0)

vals = ('op1','op2','op3') # Tuple of values
box = ttk.Combobox(win,values=vals,state='readonly') # Add value option
box.current(1) # Set the second item as current value
box.grid(column=0,row=0)

box.bind('<<ComboboxSelected>>', assign)

win.mainloop()
import tkinter as tk
from tkinter import ttk

win = tk.Tk()

label = tk.Label(win)  # Create this outside of function


def assign(event):
    value = box.get()  # Get value from combobox

    if value == vals[0]:  # Check the selected item with the values tuple
        label.config(text=value)  # Update the text of label
        label.grid(column=1, row=0)
    elif value == vals[1]:  # Check the selected item with the values tuple
        label.config(text=value)  # Update the text of label
        label.grid(column=1, row=0)
    elif value == vals[2]:  # Check the selected item with the values tuple
        label.config(text=value)  # Update the text of label
        label.grid(column=1, row=0)


vals = ('op1', 'op2', 'op3')  # Tuple of values
box = ttk.Combobox(win, values=vals, state='readonly')  # Add value option
box.current(1)  # Set the second item as current value
box.grid(column=0, row=0)

box.bind('<<ComboboxSelected>>', assign)

win.mainloop()
Related