I have not seen any post about this issue but sorry it there is one already.
I am coding a Space Invaders on Python in OOP (complete newbie in it).
I have an image in a canvas that I want to move with the keyboard arrows, so I bound a method on the Right Arrow press, which initiates a continuous movement on the right, and another when the key is released, which stops the movement.
I wanted the icon to move regularily as long as the key is held pressed so here is how I tried :
class Ship():
def __init__(self,window,canvas):
self.window = window
self.canvas = canvas
self.width = 55
self.height = 58
self.RIGHT = False
self.image_ship = ImageTk.PhotoImage(file="../Images/ship.jpg")
self.sprite_ship = self.canvas.create_image((self.canvas.winfo_width() - self.width)//2,(self.canvas.winfo_height() - self.height)-5,image = self.image_ship,anchor='nw')
def press_right(self,event):
self.RIGHT = True
self.keep_right()
def keep_right(self):
if self.RIGHT == True:
self.x = self.canvas.coords(self.sprite_ship)[0]
self.y = self.canvas.coords(self.sprite_ship)[1]
self.canvas.coords(self.sprite_ship,self.x+10,self.y)
self.window.after(1000,self.keep_right)
def stop_right(self,event):
self.RIGHT = False
But the keep_right method is called about every 0.01 second instead of 1s.
Could someone say me why so ?
question from:
https://stackoverflow.com/questions/65848778/tkinter-after-method-not-working-as-expected 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…