Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_zoom_object.py
- import tkinter as tk
- root = tk.Tk()
- canvas = tk.Canvas(root, width=400, height=400, background="bisque")
- canvas.pack(fill="both", expand=True)
- # start with one rectangle
- canvas.create_rectangle(10, 5, 75, 100, outline="", fill="blue")
- # add a new item, and apply the same scale to it
- obj = canvas.create_rectangle(75, 5, 200, 100, outline="", fill="red") # ??? outline leaves traces
- canvas.create_text(25,100, anchor="nw", text="""
- Left click and drag to move the canvas
- Scroll to zoom
- Right click to zoom all by recent""")
- current_scale = [0, 0, 1.05, 1.05]
- # move
- def move_start(event):
- canvas.scan_mark(event.x, event.y)
- def move_move(event):
- canvas.scan_dragto(event.x, event.y, gain=1)
- # zoom
- def zoom(event):
- global current_scale
- if (event.delta > 0):
- sc = -0.05
- ctr = -1
- else:
- sc = 0.05
- ctr = -1
- current_scale = [0, 0, 1+sc, 1+sc]
- canvas.scale(obj, *current_scale)
- def draw(event):
- canvas.scale("all", *current_scale)
- # Mouse bindings to the canvas
- canvas.bind("<ButtonPress-1>", move_start)
- canvas.bind("<B1-Motion>", move_move)
- root.bind_all("<MouseWheel>", zoom)
- canvas.bind("<ButtonPress-3>", draw)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement