0

I am new to tkinter, and I would like to start a kind of loop, from the moment I pressed a key on my keyboard. Only, when I call my first function, I have an error:

TypeError: anime_avancer () missing 1 required positional argument: 'event'.

I understand that there is something to do with the bind method, but I do not understand the structure ... Thank you in advance !

from tkinter import *

fen=Tk()
can=Canvas(fen,bg="light gray", height=500, width=500)
can.pack()

def afficher_codeur():
    #code here
    anime_avancer()

def anime_avancer(event):
    #code here#
    afficher_codeur()

fen.bind("<Right>", anime_avancer)

fen.mainloop()

I would like to have explanations on the role of the "event" if possible, thanks ! ;)

2 Answers2

1

Your anime_avancer callback has a call to afficher_codeur that calls itself anime_avancer again, but with no arguments instead of one. Hence the runtime error.

If you want at some point to call your anime_avancer method but without any argument, simply call anime_avancer(None).

event in a Tkinter canvas callback is a positional argument that stores various informations about the state of your keyboard & mouse when the event is fired. For instance, event.x and event.y store the position of your mouse.

See the complete documentation for Canvas.bind() here.

A complete example on how to use a callback method in Tk is available here.

Antoine C.
  • 3,730
  • 5
  • 32
  • 56
0

Thanks to Antoine C, the answer was to put "None" as an argument of the anime_avancer function in the afficher_codeur loop!