[Python] How to use a <ComboboxSelected> virtual event with tkinter

Discussão em 'Python' iniciado por Stack, Junho 27, 2025.

  1. Stack

    Stack Membro Participativo

    I am using a tkk.Combobox themed widget in Python 3.5.2. I want an action to happen when a value is selected.

    In the Python docs, it says:


    The combobox widgets generates a <<ComboboxSelected>> virtual event when the user selects an element from the list of values.
    ​

    Here on the Stack, there are a number of answers (1, 2, etc) that show how to bind the event:

    cbox.bind("<<ComboboxSelected>>", function)


    However, I can't make it work. Here's a very simple example demonstrating my non-functioning attempt:

    import tkinter as tk
    from tkinter import ttk

    tkwindow = tk.Tk()

    cbox = ttk.Combobox(tkwindow, values=[1,2,3], state='readonly')
    cbox.grid(column=0, row=0)

    cbox.bind("<<ComboboxSelected>>", print("Selected!"))

    tkwindow.mainloop()


    I get one instance of "Selected!" immediately when I run this code, even without clicking anything. But nothing happens when I actually select something in the combobox.

    I'm using IDLE in Windows 7, in case it makes a difference.

    What am I missing?

    Continue reading...

Compartilhe esta Página