Tkinter: How to update widgets using value selected in Combobox?
To get value selected in Combobox
and use it to update value in Label
you can use bind()
with (virtual) event '<<ComboboxSelected>>'
.
It can be used to assing function which will be executed you select value in Combobox
- and this function may update value in Label
import tkinter as tk
import tkinter.ttk as ttk
# --- functions ---
def on_select(event):
#print('[DEBUG] event:', event)
#print('[DEBUG] event.widget:', event.widget)
#print('[DEBUG] event.widget.get():', event.widget.get())
#print('---')
selected = event.widget.get()
label['text'] = selected
# --- main ---
root = tk.Tk()
combo = ttk.Combobox(root, values=['A', 'B', 'C'])
combo.pack()
combo.bind('<<ComboboxSelected>>', on_select)
label = tk.Label(root) # can be without any text or ie. with `?`
label.pack()
root.mainloop()
Firts you create Combobox
combo = ttk.Combobox(root, values=['A', 'B', 'C'])
combo.pack()
Next you use bind()
to assign function's name - ie. on_select
- (without ()
and arguments) to event '<<ComboboxSelected>>'
in widget Combobox
combo.bind('<<ComboboxSelected>>', on_select)
And finally create function on_select
which gets one argument - ie. event
- with information about event.
In event
you may have different information - for example event.widget
gives access to widget Combobox
which was used.
This way you could assing the same function to two different Combobox
and get access to correct widget.
def on_select(event):
#print('[DEBUG] event:', event)
#print('[DEBUG] event.widget:', event.widget)
#print('[DEBUG] event.widget.get():', event.widget.get())
#print('---')
selected = event.widget.get()
label['text'] = selected
And here more complex example which uses Combobox
to update values in Label
, Button
, Entry
, Listbox
, Text
and even in another Combobox
import tkinter as tk
import tkinter.ttk as ttk
# --- functions ---
def on_select(event):
print('[DEBUG] event:', event)
print('[DEBUG] event.widget:', event.widget)
print('[DEBUG] event.widget.get():', event.widget.get())
print('---')
selected = event.widget.get()
label['text'] = selected
button['text'] = selected
entry.delete('0', 'end') # remove previous content
entry.insert('end', selected)
combobox2_values = {
'A': ['A1', 'A2', 'A3'],
'B': ['B1', 'B2', 'B3'],
'C': ['C1', 'C2', 'C3'],
}
combobox2['values'] = combobox2_values[selected]
listbox.delete(0, 'end') # remove previous content
for item in combobox2_values[selected]:
listbox.insert('end', item)
values = combobox2_values[selected]
values_str = ', '.join(values)
#text.delete('1.0', 'end') # remove previous content
text.insert('end', selected + ': ' + values_str + '\n')
# --- main ---
root = tk.Tk()
combobox = ttk.Combobox(root, values=['A', 'B', 'C'])
combobox.pack()
combobox.bind('<<ComboboxSelected>>', on_select)
# --- other widgets ---
label = tk.Label(root, text='?')
label.pack()
button = tk.Button(root, text='?')
button.pack()
entry = tk.Entry(root)
entry.pack()
#entry.insert(0, '?')
#entry.insert('end', '?')
combobox2 = ttk.Combobox(root)
combobox2.pack()
listbox = tk.Listbox(root)
listbox.pack()
text = tk.Text(root, width=30, height=10)
text.pack()
#text.insert('1.0', '?\n')
#text.insert('end', '?\n')
# ---
root.mainloop()
Notes:
-
Different widgets may use different events and most of them use single
< >
in its name. -
Function assigned with
bind()
is always executed with information about event - so it has to get this information - using argument ie.event
. -
<<ComboboxSelected>>
event needs double<< >>
. In some documentations event with double<< >>
is calledvirtual event
. -
Function's name without
()
(and without arguments) which is assigned as argument in other function (which will execute it later) often is called "callback". This name is used not only intkinter
andPython
but also in other modules and in other languages. -
In
tkinter
callback is used inbind(event, callback)
,after(milisekundy, callback)
,tk.Button(..., command=callback)
and in few other widgets