Advertisement
here2share

# Tk_scroll_by_mouse_angle.py

Aug 12th, 2022
1,188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.90 KB | None | 0 0
  1. # Tk_scroll_by_mouse_angle.py
  2.  
  3. # ZZZ
  4.  
  5. from tkinter import *
  6. from math import *
  7. from time import *
  8.  
  9. def mouse_down(e):
  10.     cv.px = int(e.x/unit) * unit
  11.     cv.py = int(e.y/unit) * unit
  12. def mouse_up(e):
  13.     cv.x = 0
  14.     cv.y = 0
  15. def mouse_move(e):
  16.     cv.x = e.x
  17.     cv.y = e.y
  18. def scroll_dist():
  19.     return sqrt((cv.x - cv.width/2)**2 + (cv.y - cv.height/2)**2)
  20. def draw_grid():
  21.     for x in range(0, sq_units, unit*2):
  22.         canvas.create_line([(x, 0), (x, sq_units)], fill='lightblue', tags=('mv','g'))
  23.     for y in range(0, sq_units, unit*2):
  24.         canvas.create_line([(0, y), (sq_units, y)], fill='lightblue', tags=('mv','g'))
  25. root = Tk()
  26. root.geometry('+-10+0')
  27. root.title("Tk_scroll_by_mouse_angle")
  28.  
  29. class Cv(): 0
  30. cv = Cv()
  31.  
  32. cv.x = 0
  33. cv.y = 0
  34. cv.px = 0
  35. cv.py = 0
  36. cv.x2 = 0
  37. cv.y2 = 0
  38. cv.prev = 0
  39. cv.timer = time()
  40.  
  41. unit = 50
  42.  
  43. sq_units = 10000
  44.  
  45. cv.width = 1280
  46. cv.height = 640
  47.  
  48. cv.ww = 4000
  49. cv.hh = 3000
  50.  
  51. vectors = []
  52.  
  53. frame = Frame(root)
  54. frame.pack()
  55.  
  56. # create canvas with scrollbars
  57. canvas = Canvas(frame, width=cv.width, height=cv.height, bg='white')
  58. canvas.pack(side=LEFT, expand=True, fill=BOTH)
  59. canvas.pack_propagate(False)
  60.  
  61. draw_grid()
  62.  
  63. # create scrollbars
  64. vbar = Scrollbar(canvas)
  65. vbar.pack(side=RIGHT, fill=Y)
  66. hbar = Scrollbar(canvas, orient=HORIZONTAL)
  67. hbar.pack(side=BOTTOM, fill=X)
  68.  
  69. # attach canvas to scrollbars
  70. canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
  71.  
  72. # bind scrollbars to the canvas
  73. hbar.config(command=canvas.xview)
  74. vbar.config(command=canvas.yview)
  75.  
  76. # set the scrollregion to all
  77. canvas.config(scrollregion=(0,0,sq_units,sq_units))
  78. '''
  79. '''
  80.  
  81. canvas.bind("<Button-1>", mouse_down)
  82. canvas.bind("<B1-Motion>", mouse_move)
  83. canvas.bind("<ButtonRelease-1>", mouse_up)
  84.  
  85. '''
  86.             canvas.move('mv', cv.x2, cv.y2)
  87.             vectors.append((cx+cv.x, cy+cv.y))
  88.             canvas.update()
  89.     canvas.after(1, move_canvas)
  90.  
  91. move_canvas()
  92.  
  93. root.mainloop()
  94. '''
  95.  
  96. # to keep moving canvas by mouse position from center of the screen while it draws a line
  97. def move_canvas():
  98.     if cv.x:
  99.         canvas.delete('line')
  100.         canvas.delete('coord')
  101.         cv.timer = time()          
  102.         cx, cy,_ ,_ = canvas.coords('g')
  103.         cx, cy = cx*-1, cy*-1
  104.         cv.x2 = (cv.x - cv.width/2)*-0.2
  105.         cv.y2 = (cv.y - cv.height/2)*-0.2
  106.         canvas.create_text(cv.x, cv.y+10, text=list(map(int ,(cx, cy, cv.x2, cv.y2))), tags=('coord'))
  107.         # move canvas with cv.x2 and cv.y2 while keeping the coords within (0, 0, cv.ww, cv.hh)
  108.         if cx-cv.x2 < 0:
  109.             cv.x2 = cx
  110.         elif cx-cv.x2 > cv.ww:
  111.             0
  112.             # cv.x2 = cv.ww ### ???
  113.         if cy-cv.y2 < 0:
  114.             cv.y2 = -cx
  115.         elif cy-cv.y2 > cv.hh:
  116.             0
  117.             # cv.y2 = cv.hh ### ???
  118.         canvas.move('mv', cv.x2, cv.y2)
  119.         vectors.append((cv.x, cv.y))
  120.         #canvas.place_configure('mv', x=cv.x2, y=cv.y2)
  121.         try:
  122.             canvas.create_line(vectors, fill='red', tags=('mv', 'line'))
  123.         except: 0
  124.         cv.px, cv.py = cx+cv.x, cy+cv.y
  125.         canvas.update()
  126.     canvas.after(100, move_canvas)
  127.  
  128. move_canvas()
  129.  
  130. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement