[Python] Python type-hinting: a Tkinter Event

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

  1. Stack

    Stack Membro Participativo

    I'm learning how to create GUIs with TKinter in Python. I'm using VSCode as my editor and I have this piece of code:

    [...]
    # create a button and bind an action to the button press event
    button:tk.Button = tk.Button(master=gridFrame, text="Button")
    button.bind("<ButtonPress-1>", self.play)

    [...]
    def play(self, event: tk.Event)->None:
    """Hanlde a player's move"""
    clickedBtn: tk.Button = event.widget


    Pylance complains on the type hint for event being of type Event: Expected type arguments for generic class "Event". I've tried with this other hint:

    def play(self, event: tk.Event[tk.Button])->None:


    Which silences the warning, but when I try to run the code with this, I get a TypeError exception:

    def play(self, event: Event[tk.Button])->None:
    ~~~~~^^^
    TypeError: type 'Event' is not subscriptable


    Is there a way to hint this or should I settle for Any or silence the warning and admit defeat?

    Thanks!

    Continue reading...

Compartilhe esta Página