0

func() missing 1 required positional argument: 'event'

why this code gives that error and how to bind a keyboard key to a function in the right way from tkinter import* w=Tk() c=Canvas(w,bg='black',width=300,height=300) c.grid() sk=c.create_rectangle(5, 5, 25, 25, fill = "red") def mv(event): c.move(sk,1,0) w.after(50,mv) w.bind('<Button-1>',mv)

24th Jan 2020, 5:05 PM
Pattern
Pattern - avatar
4 Respostas
+ 4
Here is a fix to your code: https://code.sololearn.com/cnFKS3MyYXI5/?ref=app Try to write clean code next time. Readability is important, for both you and others who may read your code. Here are you mistakes: 1. You did not use the window.mainloop() 2. In the line window.after(50,mv), you were missing the 'event' argument, thus the error. You must use a lambda function in order to pass the argument to the function, like this: windows.after(50, lambda: mv(event))
24th Jan 2020, 5:39 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 3
It is a very bad habit to use abbreviations in programming, unless it's a really small script. What is sk for ?
24th Jan 2020, 5:33 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
1. You forgot the mainloop 2. I think the problem is coming from the last line inside the function. When you run w.after(50, mv), you are not giving any parameters to it, thus the error.
24th Jan 2020, 5:22 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
I don't understand what should I do to fix the problem
24th Jan 2020, 5:26 PM
Pattern
Pattern - avatar