Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_rotate_oval_control.py
- from Tkinter import *
- root = Tk()
- root.title("Tk Rotate Oval Control")
- ### root.withdraw() # vs root.deiconify()')
- xm,ym=600,600
- canvas = Canvas(root, width=xm, height=ym)
- canvas.grid()
- from PIL import ImageDraw, ImageTk, Image, ImageGrab
- from random import randrange
- import time
- class Cv(): pass
- cv=Cv()
- 0
- RGB_BLACK=(0,0,0)
- RGB_WHITE=(255,255,255)
- RGB_GRAY=(180,180,180)
- RGB_GRAYLIGym=(236,236,236)
- RGB_GRAYDARK=(169,169,169)
- RGB_RED=(255,0,0)
- RGB_ORANGE=(255,165,0)
- RGB_YELLOW=(255,255,0)
- RGB_GREEN=(0,128,0)
- RGB_BLUE=(0,0,255)
- RGB_CYAN=(0,255,255)
- RGB_PURPLE=(128,0,128)
- RGB_DARKBLUE=(0,0,139)
- RGB_DARKGREEN=(0,100,0)
- RGB_DEEPPINK=(255,20,147)
- RGB_INDIGO=(75,0,130)
- RGB_LIGymPURPLE=(204,153,255)
- RGB_LIGymBLUE=(173,216,230)
- RGB_LIGymGREEN=(178,255,102)
- RGB_LIGymYELLOW=(255,255,102)
- RGB_LIME=(0,255,0)
- RGB_OLIVE=(107,142,35)
- RGB_BROWN=(139,69,19)
- RGB_GOLD=(255,215,0)
- RGB_SILVER=(192,192,192)
- RGB_ROYALBLUE=(65,105,225)
- 0
- img = Image.new('RGB', (xm,ym))
- # random.seed(1)
- from math import sin, cos, pi
- def rotate_oval( x1, y1, x2, y2, rotate=0, vertex_count=-1):
- rotate=rotate%180
- if vertex_count == -1:
- vertex_count = int((abs(x1 - x2) + abs(y1 - y2)) * 0.07)
- vertex_count = max(18,vertex_count)
- vertex_count = max(3,vertex_count)
- sin(pi/180*rotate)
- cos(pi/180*rotate)
- (x1, x2) = (min(x1, x2), max(x1, x2))
- (y1, y2) = (min(y1, y2), max(y1, y2))
- a = (x2 - x1) / 2
- b = (y2 - y1) / 2
- VERTEX = [ (a * cos(i * 2 * pi / vertex_count), \
- b * sin(i * 2 * pi / vertex_count)) \
- for i in range(vertex_count) ]
- ## rotation
- VERTEX = [( x*cos(pi/180*rotate) + y*sin(pi/180*rotate), y*cos(pi/180*rotate) - x*sin(pi/180*rotate) )
- for (x,y) in VERTEX ]
- ## move center
- VERTEX = [( x + ( x1 + x2 ) / 2, y + ( y1 + y2 ) / 2 )
- for (x,y) in VERTEX ]
- return VERTEX
- x1, y1, x2, y2 = 200, 0, 400, 500
- angle = 0
- speed = 10
- SlideTest = 1
- if SlideTest:
- go = 0
- root2 = Tk()
- root2.title("Tk Qwikfix Sliders")
- vars_init = x1, y1, x2, y2, speed
- vars_str = 'x1 y1 x2 y2 speed'.split()
- def refresh():
- try:
- canvas.update()
- except:
- pass
- def getSliderValues(nil=0):
- if go:
- sliderVector = []
- for z in range(len(cvars)):
- t = w[z].get()
- sliderVector.append('%.2f'%(float(t)))
- # print '\t\t'.join(sliderVector)
- return [float(z) for z in sliderVector]
- sliderframes = Frame(root2)
- sliderframes.pack(side=TOP)
- slider = {}
- cvars = {}
- iframe = Frame(root2)
- iframe.pack(side=BOTTOM)
- w = {}
- for z in range(len(vars_init)):
- slider[z] = Frame(sliderframes)
- slider[z].pack(side=TOP)
- cvars[z] = DoubleVar()
- FROM, TO, L = -500, 500, 1000
- w[z] = Scale(slider[z], from_=FROM, to=TO, variable=cvars[z], length=L, orient=HORIZONTAL, command=getSliderValues)
- w[z].set(vars_init[z])
- w[z].pack(side=TOP)
- L = Label(slider[z], text=vars_str[z])
- L.pack(side=TOP)
- getSliderValues()
- go = 1
- while go:
- canvas.delete('all')
- x1, y1, x2, y2, speed = getSliderValues()
- angle += speed
- roto = rotate_oval(x1, y1, x2, y2, angle*0.001)
- canvas.create_polygon(roto)
- root.update()
- root.mainloop()
Add Comment
Please, Sign In to add comment