here2share

# Tk_rotate_oval_control.py

Jun 24th, 2020
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.29 KB | None | 0 0
  1. # Tk_rotate_oval_control.py
  2.  
  3. from Tkinter import *
  4.  
  5. root = Tk()
  6. root.title("Tk Rotate Oval Control")
  7. ### root.withdraw() # vs root.deiconify()')
  8. xm,ym=600,600
  9.  
  10. canvas = Canvas(root, width=xm, height=ym)
  11. canvas.grid()
  12.  
  13. from PIL import ImageDraw, ImageTk, Image, ImageGrab
  14. from random import randrange
  15. import time
  16.  
  17. class Cv(): pass
  18. cv=Cv()
  19.  
  20. 0
  21. RGB_BLACK=(0,0,0)
  22. RGB_WHITE=(255,255,255)
  23. RGB_GRAY=(180,180,180)
  24.  
  25. RGB_GRAYLIGym=(236,236,236)
  26. RGB_GRAYDARK=(169,169,169)
  27.  
  28. RGB_RED=(255,0,0)
  29. RGB_ORANGE=(255,165,0)
  30. RGB_YELLOW=(255,255,0)
  31. RGB_GREEN=(0,128,0)
  32. RGB_BLUE=(0,0,255)
  33. RGB_CYAN=(0,255,255)
  34. RGB_PURPLE=(128,0,128)
  35.  
  36. RGB_DARKBLUE=(0,0,139)
  37. RGB_DARKGREEN=(0,100,0)
  38. RGB_DEEPPINK=(255,20,147)
  39. RGB_INDIGO=(75,0,130)
  40. RGB_LIGymPURPLE=(204,153,255)
  41. RGB_LIGymBLUE=(173,216,230)
  42. RGB_LIGymGREEN=(178,255,102)
  43. RGB_LIGymYELLOW=(255,255,102)
  44. RGB_LIME=(0,255,0)
  45. RGB_OLIVE=(107,142,35)
  46.  
  47. RGB_BROWN=(139,69,19)
  48. RGB_GOLD=(255,215,0)
  49. RGB_SILVER=(192,192,192)
  50.  
  51. RGB_ROYALBLUE=(65,105,225)
  52. 0
  53. img = Image.new('RGB', (xm,ym))
  54. # random.seed(1)
  55.  
  56.  
  57. from math import sin, cos, pi
  58.  
  59. def rotate_oval( x1, y1, x2, y2, rotate=0, vertex_count=-1):
  60.     rotate=rotate%180
  61.     if vertex_count == -1:
  62.         vertex_count = int((abs(x1 - x2) + abs(y1 - y2)) * 0.07)
  63.         vertex_count = max(18,vertex_count)
  64.     vertex_count = max(3,vertex_count)
  65.     sin(pi/180*rotate)
  66.     cos(pi/180*rotate)
  67.     (x1, x2) = (min(x1, x2), max(x1, x2))
  68.     (y1, y2) = (min(y1, y2), max(y1, y2))
  69.     a = (x2 - x1) / 2
  70.     b = (y2 - y1) / 2
  71.     VERTEX = [ (a * cos(i * 2 * pi / vertex_count), \
  72.                 b * sin(i * 2 * pi / vertex_count)) \
  73.                     for i in range(vertex_count) ]
  74.     ## rotation
  75.     VERTEX = [( x*cos(pi/180*rotate) + y*sin(pi/180*rotate), y*cos(pi/180*rotate) - x*sin(pi/180*rotate) )
  76.               for (x,y) in VERTEX  ]
  77.  
  78.     ## move center
  79.     VERTEX = [( x + ( x1 + x2 ) / 2, y + ( y1 + y2 ) / 2 )
  80.               for (x,y) in VERTEX  ]
  81.     return VERTEX
  82.  
  83. x1, y1, x2, y2 = 200, 0, 400, 500
  84. angle = 0
  85. speed = 10
  86.  
  87. SlideTest = 1
  88.  
  89. if SlideTest:
  90.     go = 0
  91.     root2 = Tk()
  92.     root2.title("Tk Qwikfix Sliders")
  93.     vars_init = x1, y1, x2, y2, speed
  94.     vars_str = 'x1 y1 x2 y2 speed'.split()
  95.  
  96.     def refresh():
  97.         try:
  98.             canvas.update()
  99.         except:
  100.             pass
  101.  
  102.     def getSliderValues(nil=0):
  103.         if go:
  104.             sliderVector = []
  105.             for z in range(len(cvars)):
  106.                 t = w[z].get()
  107.                 sliderVector.append('%.2f'%(float(t)))
  108.             # print '\t\t'.join(sliderVector)
  109.             return [float(z) for z in sliderVector]
  110.  
  111.     sliderframes = Frame(root2)
  112.     sliderframes.pack(side=TOP)
  113.  
  114.     slider = {}
  115.     cvars = {}
  116.  
  117.     iframe = Frame(root2)
  118.     iframe.pack(side=BOTTOM)
  119.  
  120.     w = {}
  121.     for z in range(len(vars_init)):
  122.         slider[z] = Frame(sliderframes)
  123.         slider[z].pack(side=TOP)
  124.         cvars[z] = DoubleVar()
  125.         FROM, TO, L = -500, 500, 1000
  126.         w[z] = Scale(slider[z], from_=FROM, to=TO, variable=cvars[z], length=L, orient=HORIZONTAL, command=getSliderValues)
  127.         w[z].set(vars_init[z])
  128.         w[z].pack(side=TOP)
  129.         L = Label(slider[z], text=vars_str[z])
  130.         L.pack(side=TOP)
  131.  
  132.     getSliderValues()
  133.  
  134. go = 1
  135. while go:
  136.     canvas.delete('all')
  137.    
  138.     x1, y1, x2, y2, speed = getSliderValues()
  139.     angle += speed
  140.  
  141.     roto = rotate_oval(x1, y1, x2, y2, angle*0.001)
  142.     canvas.create_polygon(roto)
  143.     root.update()
  144.  
  145.  
  146. root.mainloop()
Add Comment
Please, Sign In to add comment