Advertisement
here2share

# Tk_drag_canvas.py

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