# move_object_tk.py3 # imports every file form tkinter from tkinter import * class GFG: def __init__(self, master = None): self.master = master print("in __init__") # to take care movement in x direction self.x = 3 # amount to move # to take care movement in y direction self.y = 3 # amount to move self.count = 0 self.time_step = 100 # # canvas object to create shape self.canvas = Canvas(master, width=500, height=500) # creating rectangle self.canvas.create_text(200, 20, text='arrow keys change direction', fill='black') self.canvas.create_text(200, 30, text='right click=bigger step, left click=smaller step', fill='black') self.canvas.create_text(200, 40, text='key + =faster, key -= slower', fill='black') self.rectangle = self.canvas.create_rectangle( 100, 100, 150, 150, fill = "red") self.canvas.pack() # # calling class's movement method to # move the rectangle print("self.movement called") self.movement() # end _init_ def movement(self): # This is where the move() method is called # This moves the rectangle to x, y coordinates print("in def movement, count=",self.count," x=",self.x," y=",self.y," t=",self.time_step) self.count = self.count + 1 if self.count > 50: self.count = 0 self.x = - self.x # reverse direction self.y = - self.y #self.rectangle = self.canvas.create_rectangle( # 100, 100, 150, 150, fill = "blue") # end if self.canvas.move(self.rectangle, self.x, self.y) self.canvas.after(self.time_step, self.movement) # now slower, was 20 # end movement # for motion in negative x direction def left(self, event): print(event.keysym) self.x = -5 # amount to move self.y = 0 # end left # for motion in positive x direction def right(self, event): print(event.keysym) self.x = 5 # amount to move self.y = 0 # end right # for motion in positive y direction def up(self, event): print(event.keysym) self.x = 0 self.y = -5 # amount to move # end up # for motion in negative y direction def down(self, event): print(event.keysym) self.x = 0 self.y = 5 # amount to move # end down # for motion to go faster, shorter time def faster(self, event): print("speed faster") self.time_step = self.time_step /2 # end faster # for motion to go faster, shorter time def slower(self, event): print("speed slower") self.time_step = self.time_step * 2 # end slower # for step size smaller def mouseB3press(self, event): # right print("right 3 mouse pressed") if self.x > 0 : self.x = self.x - 2 # change amount to move each time # end if if self.x < 0 : self.x = self.x + 2 # end if if self.y > 0 : self.y = self.y - 2 # amount to move # end if if self.y < 0 : self.y = self.y + 2 # amount to move # end if # end mouseB3press # for step size bigger def mouseB1press(self, event): # left print("left 1 mouse pressed") if self.x > 0 : self.x = self.x + 2 # end if if self.x < 0 : self.x = self.x - 2 # end if if self.y > 0 : self.y = self.y + 2 # amount to move # end if if self.y < 0 : self.y = self.y - 2 # amount to move # end if # end mouseB1press # end class GFG if __name__ == "__main__": # object of class Tk, responsible for creating # a tkinter toplevel window print("move_object_tk.py3 running") master = Tk() gfg = GFG(master) # # This will bind arrow keys to the tkinter # toplevel which will navigate the image or drawing master.bind("", lambda e: gfg.left(e)) master.bind("", lambda e: gfg.right(e)) master.bind("", lambda e: gfg.up(e)) master.bind("", lambda e: gfg.down(e)) master.bind("", lambda e: gfg.faster(e)) master.bind("", lambda e: gfg.slower(e)) # This will bind mouse press to the tkinter master.bind('', lambda e: gfg.mouseB1press(e)) master.bind('', lambda e: gfg.mouseB3press(e)) # # Infinite loop breaks only by interrupt, click # mainloop() # end main