Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_scroll_by_mouse_angle.py
- # ZZZ
- from tkinter import *
- from math import *
- from time import *
- def mouse_down(e):
- cv.px = int(e.x/unit) * unit
- cv.py = int(e.y/unit) * unit
- def mouse_up(e):
- cv.x = 0
- cv.y = 0
- def mouse_move(e):
- cv.x = e.x
- cv.y = e.y
- def scroll_dist():
- return sqrt((cv.x - cv.width/2)**2 + (cv.y - cv.height/2)**2)
- def draw_grid():
- for x in range(0, sq_units, unit*2):
- canvas.create_line([(x, 0), (x, sq_units)], fill='lightblue', tags=('mv','g'))
- for y in range(0, sq_units, unit*2):
- canvas.create_line([(0, y), (sq_units, y)], fill='lightblue', tags=('mv','g'))
- root = Tk()
- root.geometry('+-10+0')
- root.title("Tk_scroll_by_mouse_angle")
- class Cv(): 0
- cv = Cv()
- cv.x = 0
- cv.y = 0
- cv.px = 0
- cv.py = 0
- cv.x2 = 0
- cv.y2 = 0
- cv.prev = 0
- cv.timer = time()
- unit = 50
- sq_units = 10000
- cv.width = 1280
- cv.height = 640
- cv.ww = 4000
- cv.hh = 3000
- vectors = []
- frame = Frame(root)
- frame.pack()
- # create canvas with scrollbars
- canvas = Canvas(frame, width=cv.width, height=cv.height, bg='white')
- canvas.pack(side=LEFT, expand=True, fill=BOTH)
- canvas.pack_propagate(False)
- draw_grid()
- # create scrollbars
- vbar = Scrollbar(canvas)
- vbar.pack(side=RIGHT, fill=Y)
- hbar = Scrollbar(canvas, orient=HORIZONTAL)
- hbar.pack(side=BOTTOM, fill=X)
- # attach canvas to scrollbars
- canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
- # bind scrollbars to the canvas
- hbar.config(command=canvas.xview)
- vbar.config(command=canvas.yview)
- # set the scrollregion to all
- canvas.config(scrollregion=(0,0,sq_units,sq_units))
- '''
- '''
- canvas.bind("<Button-1>", mouse_down)
- canvas.bind("<B1-Motion>", mouse_move)
- canvas.bind("<ButtonRelease-1>", mouse_up)
- '''
- canvas.move('mv', cv.x2, cv.y2)
- vectors.append((cx+cv.x, cy+cv.y))
- canvas.update()
- canvas.after(1, move_canvas)
- move_canvas()
- root.mainloop()
- '''
- # to keep moving canvas by mouse position from center of the screen while it draws a line
- def move_canvas():
- if cv.x:
- canvas.delete('line')
- canvas.delete('coord')
- cv.timer = time()
- cx, cy,_ ,_ = canvas.coords('g')
- cx, cy = cx*-1, cy*-1
- cv.x2 = (cv.x - cv.width/2)*-0.2
- cv.y2 = (cv.y - cv.height/2)*-0.2
- canvas.create_text(cv.x, cv.y+10, text=list(map(int ,(cx, cy, cv.x2, cv.y2))), tags=('coord'))
- # move canvas with cv.x2 and cv.y2 while keeping the coords within (0, 0, cv.ww, cv.hh)
- if cx-cv.x2 < 0:
- cv.x2 = cx
- elif cx-cv.x2 > cv.ww:
- 0
- # cv.x2 = cv.ww ### ???
- if cy-cv.y2 < 0:
- cv.y2 = -cx
- elif cy-cv.y2 > cv.hh:
- 0
- # cv.y2 = cv.hh ### ???
- canvas.move('mv', cv.x2, cv.y2)
- vectors.append((cv.x, cv.y))
- #canvas.place_configure('mv', x=cv.x2, y=cv.y2)
- try:
- canvas.create_line(vectors, fill='red', tags=('mv', 'line'))
- except: 0
- cv.px, cv.py = cx+cv.x, cy+cv.y
- canvas.update()
- canvas.after(100, move_canvas)
- move_canvas()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement