Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_drag_all.py
- from tkinter import *
- x = 0
- y = 0
- prevx = 0
- prevy = 0
- root = Tk()
- root.title("Tk_drag_all.py")
- canvas = Canvas(width=500, height=500, background="grey")
- canvas.grid(row=0, column=0, sticky="nsew")
- def btn_1(event):
- global x, y, prevx, prevy
- prevx = x = event.x
- prevy = y = event.y
- def move_every_object_except_one(event):
- global x, y, prevx, prevy, target
- x = px = event.x
- y = py = event.y
- # use prevx and prevy to get the difference between the current and the previous position
- x, y = x-prevx, y-prevy
- print (x,y)
- prevx = px
- prevy = py
- target = (target[0]+x, target[1]+y, target[2]+x, target[3]+y)
- draw()
- # This is what enables scrolling with the mouse...
- canvas.bind("<ButtonPress-1>", btn_1)
- canvas.bind("<B1-Motion>", move_every_object_except_one)
- colors = "red", "orange", "yellow", "green", "blue"
- target = (0,0,1000,1000)
- sz = 100
- def draw():
- global target
- t = target
- canvas.delete('all')
- for n in colors:
- canvas.create_oval(t, outline=n, fill=n)
- t = t[0]+sz, t[1]+sz, t[2]-sz, t[3]-sz
- canvas.create_text(50,10, anchor="nw",
- text="CLICK AND DRAG CANVAS TO MOVE OBJECTS")
- canvas.update()
- draw()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement