Advertisement
here2share

# Tk_xy_degrees_and_distance.py

Sep 9th, 2020 (edited)
1,515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. # Tk_xy_degrees_and_distance.py
  2.  
  3. from Tkinter import *
  4. from PIL import ImageDraw, ImageTk, Image, ImageGrab
  5. import math
  6. import random
  7.  
  8. root = Tk()
  9. root.title("Tk_xy_degrees_and_distance".replace('_',' '))
  10. ### root.withdraw() # vs root.deiconify()')
  11. xx,yy = 600,600
  12.  
  13. canvas = Canvas(root, width=xx, height=yy)
  14. canvas.grid()
  15.  
  16. mouse = [xx/2,yy/2]
  17.  
  18. def mmove(event):
  19.     mouse[:] = [z*1.0 for z in (event.x,event.y)]
  20. canvas.bind('<Motion>',mmove)
  21.    
  22. def dist(a,b):
  23.     return (abs(a)**2+abs(b)**2)**0.5
  24. def degrees(x1,y1,x2,y2):
  25.     return (math.degrees(math.atan2((x2-x1),(y2-y1)*-1))+90)%360 if (y2-y1) else 0.0
  26.  
  27. while 1:
  28.     canvas.delete('all')
  29.     xm, ym = mouse
  30.     deg = degrees(xx/2, yy/2, xm, ym)
  31.     distance = dist(xx/2-xm, yy/2-ym)
  32.     canvas.create_line(xx/2, yy/2, xm, ym, fill='blue')
  33.     canvas.create_text(20, 20, anchor='nw', font='simsun 20', text='degrees: %f'%deg)
  34.     canvas.create_text(20, 50, anchor='nw', font='simsun 20', text='distance: %f'%distance)
  35.     t = 'x: %d y: %d +++(%d,%d)'%(xm, ym, xm-xx/2, ym-yy/2)
  36.     canvas.create_text(20, 80, anchor='nw', font='simsun 20', text=t)
  37.     canvas.update()
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement