Advertisement
here2share

# Tk_drag_canvas.py

Nov 26th, 2022 (edited)
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. # Tk_drag_canvas.py
  2.  
  3. from tkinter import *
  4.  
  5. root = Tk()
  6. root.title("Tk_drag_canvas.py")
  7.  
  8. def scroll_start(event):
  9.     canvas.scan_mark(event.x, event.y)
  10.  
  11. def scroll_move(event):
  12.     canvas.scan_dragto(event.x, event.y, gain=1)
  13.  
  14. canvas = Canvas(width=400, height=400, background="grey")
  15. xsb = Scrollbar(orient="horizontal", command=canvas.xview)
  16. ysb = Scrollbar(orient="vertical", command=canvas.yview)
  17. canvas.configure(yscrollcommand=ysb.set, xscrollcommand=xsb.set)
  18. canvas.configure(scrollregion=(0,0,1000,1000))
  19.  
  20. xsb.grid(row=1, column=0, sticky="ew")
  21. ysb.grid(row=0, column=1, sticky="ns")
  22. canvas.grid(row=0, column=0, sticky="nsew")
  23.  
  24. colors = "red", "orange", "yellow", "green", "blue"
  25. target = (0,0,1000,1000)
  26. sz = 100
  27. for n in colors:
  28.     canvas.create_oval(target, outline=n, fill=n)
  29.     target = target[0]+sz, target[1]+sz, target[2]-sz, target[3]-sz
  30. canvas.create_text(50,10, anchor="nw",
  31.                         text="CLICK AND DRAG TO MOVE THE CANVAS")
  32.  
  33. # This is what enables scrolling with the mouse...
  34. canvas.bind("<ButtonPress-1>", scroll_start)
  35. canvas.bind("<B1-Motion>", scroll_move)
  36.  
  37. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement