Advertisement
here2share

# Tk_zoom_object.py

Jan 15th, 2022
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. # Tk_zoom_object.py
  2.  
  3. import tkinter as tk
  4.  
  5. root = tk.Tk()
  6. canvas = tk.Canvas(root, width=400, height=400, background="bisque")
  7. canvas.pack(fill="both", expand=True)
  8.  
  9. # start with one rectangle
  10. canvas.create_rectangle(10, 5, 75, 100, outline="", fill="blue")
  11.  
  12. # add a new item, and apply the same scale to it
  13. obj = canvas.create_rectangle(75, 5, 200, 100, outline="", fill="red") # ??? outline leaves traces
  14. canvas.create_text(25,100, anchor="nw", text="""
  15.     Left click and drag to move the canvas
  16.     Scroll to zoom
  17.     Right click to zoom all by recent""")
  18.    
  19. current_scale = [0, 0, 1.05, 1.05]
  20.    
  21. # move
  22. def move_start(event):
  23.     canvas.scan_mark(event.x, event.y)
  24. def move_move(event):
  25.     canvas.scan_dragto(event.x, event.y, gain=1)
  26.  
  27. # zoom
  28. def zoom(event):
  29.     global current_scale
  30.     if (event.delta > 0):
  31.         sc = -0.05
  32.         ctr = -1
  33.     else:
  34.         sc = 0.05
  35.         ctr = -1
  36.     current_scale = [0, 0, 1+sc, 1+sc]
  37.     canvas.scale(obj, *current_scale)
  38.  
  39. def draw(event):
  40.     canvas.scale("all", *current_scale)
  41.  
  42. # Mouse bindings to the canvas
  43. canvas.bind("<ButtonPress-1>", move_start)
  44. canvas.bind("<B1-Motion>", move_move)
  45. root.bind_all("<MouseWheel>", zoom)
  46. canvas.bind("<ButtonPress-3>", draw)
  47.  
  48. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement