Advertisement
here2share

# Tk_drag_all.py

Nov 26th, 2022
1,007
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. # Tk_drag_all.py
  2.  
  3. from tkinter import *
  4.  
  5. x = 0
  6. y = 0
  7.  
  8. prevx = 0
  9. prevy = 0
  10.  
  11. root = Tk()
  12. root.title("Tk_drag_all.py")
  13.  
  14. canvas = Canvas(width=500, height=500, background="grey")
  15. canvas.grid(row=0, column=0, sticky="nsew")
  16.  
  17.  
  18. def btn_1(event):
  19.     global x, y, prevx, prevy
  20.     prevx = x = event.x
  21.     prevy = y = event.y
  22.  
  23. def move_every_object_except_one(event):
  24.     global x, y, prevx, prevy, target
  25.     x = px = event.x
  26.     y = py = event.y
  27.    
  28.     # use prevx and prevy to get the difference between the current and the previous position
  29.     x, y =  x-prevx, y-prevy
  30.     print (x,y)
  31.     prevx = px
  32.     prevy = py
  33.    
  34.     target = (target[0]+x, target[1]+y, target[2]+x, target[3]+y)
  35.     draw()
  36.  
  37. # This is what enables scrolling with the mouse...
  38. canvas.bind("<ButtonPress-1>", btn_1)
  39. canvas.bind("<B1-Motion>", move_every_object_except_one)
  40.  
  41. colors = "red", "orange", "yellow", "green", "blue"
  42. target = (0,0,1000,1000)
  43. sz = 100
  44.  
  45. def draw():
  46.     global target
  47.     t = target
  48.     canvas.delete('all')
  49.     for n in colors:
  50.         canvas.create_oval(t, outline=n, fill=n)
  51.         t = t[0]+sz, t[1]+sz, t[2]-sz, t[3]-sz
  52.     canvas.create_text(50,10, anchor="nw",
  53.                             text="CLICK AND DRAG CANVAS TO MOVE OBJECTS")
  54.     canvas.update()
  55.  
  56. draw()
  57. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement