Advertisement
here2share

# Tk_drag_and_zoom.py

Dec 5th, 2022
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. # Tk_drag_and_zoom.py
  2.  
  3. # ZZZ almost works perfectly?
  4.  
  5. from tkinter import *
  6. from math import *
  7.  
  8. ww = 600
  9. hh = 600
  10.  
  11. fGrid = 300
  12.  
  13. fStartPanX = -ww / 2
  14. fStartPanY = -hh / 2
  15. x, y = -fStartPanX - fGrid / 2, -fStartPanY - fGrid / 2
  16.  
  17. prev_x, prev_y = x, y
  18.  
  19. dx, dy = x, y
  20.  
  21. fOffsetX = fOffsetY = 0
  22.  
  23. root = Tk()
  24. root.title("Tk_drag_and_zoom.py")
  25.  
  26. canvas = Canvas(width=ww, height=hh, background="violet")
  27. canvas.grid(row=0, column=0, sticky="nsew")
  28.  
  29. fWorldLeft = fWorldTop = fWorldRight = fWorldBottom = 0.0
  30.  
  31. Scale = 1.0
  32. zoom = 0 # I shouldn't need this
  33.  
  34. def WorldToScreen(DX, DY):
  35.     global  zoom # I shouldn't need this
  36.     XX = int(dx + DX)
  37.     YY = int(dy + DY)
  38.     if zoom: # I shouldn't need this
  39.         XX, YY = scale_to_cursor(XX, YY)
  40.         zoom = 1
  41.     return XX, YY
  42.  
  43. def btn_1(event):
  44.     global fOffsetX, fOffsetY
  45.     fOffsetX = event.x - dx
  46.     fOffsetY = event.y - dy
  47.  
  48. def motion(event):
  49.     global x, y, prev_x, prev_y
  50.     prev_x, prev_y = x, y
  51.     x = event.x
  52.     y = event.y
  53.  
  54. def drag(event):
  55.     global x, y, dx, dy
  56.     x = event.x
  57.     y = event.y
  58.     dx, dy = x - fOffsetX, y - fOffsetY
  59.     draw()
  60.  
  61. def draw():
  62.     canvas.delete('all')
  63.  
  64.     # Draw Main Axes a 10x10 Unit Grid
  65.     nLinesDrawnH = 0
  66.     nLinesDrawnV = 0
  67.     bl = br = 0
  68.     if dx+fGrid > 0 and dx < ww and dy+fGrid > 0 and dy < hh:
  69.         for xy in range(0, int(fGrid)+1, int(fGrid/9)):
  70.             if dx+xy > 0 and xy < ww:
  71.                 # Draw 10 horizontal lines
  72.                 sx, sy = WorldToScreen(xy, 0)
  73.                 ex, ey = WorldToScreen(xy, fGrid-3)
  74.                 br = ey
  75.  
  76.                 canvas.create_line(sx, sy, ex, ey, fill="black")
  77.                 nLinesDrawnH += 1
  78.            
  79.             if dy+xy > 0 and xy < hh:
  80.                 # Draw 10 vertical lines
  81.                 sx, sy = WorldToScreen(0, xy)
  82.                 ex, ey = WorldToScreen(fGrid-3, xy)
  83.                 bl = ex
  84.  
  85.                 canvas.create_line(sx, sy, ex, ey, fill="black")
  86.                 nLinesDrawnV += 1
  87.         sx, sy = WorldToScreen(0, 0)
  88.         ex, ey = WorldToScreen(fGrid, fGrid)
  89.         if bl * br:
  90.             obj = canvas.create_rectangle(sx, sy, bl, br, width=0, fill="white")
  91.             canvas.lower(obj) # vs canvas.lift(obj)
  92.             print(nLinesDrawnH, nLinesDrawnV)
  93.     else:
  94.         print('offscreen')
  95.    
  96.     canvas.update()
  97.  
  98. def ZoomIn(event):
  99.     global  dx, dy, Scale, zoom
  100.     x = event.x
  101.     y = event.y
  102.     Scale *= 1.01
  103.     zoom = 1
  104.     draw()
  105.  
  106. def ZoomOut(event):
  107.     global  dx, dy, Scale, zoom
  108.     x = event.x
  109.     y = event.y
  110.     Scale *= 0.99
  111.     zoom = 1
  112.     draw()
  113.    
  114. def scale_to_cursor(XX, YY): # ZZZ almost how I want it to function
  115.     sx = (XX - prev_x) * Scale + x
  116.     sy = (YY - prev_y) * Scale + y
  117.     return sx, sy
  118.  
  119. root.bind("<ButtonPress-1>", btn_1)
  120. root.bind("<B1-Motion>", drag)
  121. root.bind("<Motion>", motion)
  122.  
  123. root.bind("<q>", ZoomIn)
  124. root.bind("<a>", ZoomOut)
  125.  
  126. draw()
  127. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement