需求/目的:用Python+thinker实现转盘,并且能够随机旋转任意角度。
转盘形式:
主界面:
from tkinter import *
win=Tk()
win.title("大转盘")
win.geometry("300x400")
win.mainloop()
转盘绘制:
这里选择有10个选项,分别有不同的角度占比:
#画布:
canvas=Canvas(win,width=300,height=300,relief='solid')
#设置每个选项的角度:
extent_list=[15,90,30,15,30,60,15,90,15]
start_list=[]
ex = 0
for i in range(len(extent_list)):
start_list.append(ex)
ex=ex+extent_list[i]
#每个选项的颜色
color_list=['blue','orange','white','gray','green','pink','black','yellow','red']
#每个扇形的绘制:
shan_1=canvas.create_arc(30,30,270,270,extent=extent_list[0],start=start_list[0],fill='blue',style=PIESLICE)
shan_2=canvas.create_arc(30,30,270,270,extent=extent_list[1],start=start_list[1],fill='orange',style=PIESLICE)
shan_3=canvas.create_arc(30,30,270,270,extent=extent_list[2],start=start_list[2],fill='white',style=PIESLICE)
shan_4=canvas.create_arc(30,30,270,270,extent=extent_list[3],start=start_list[3],fill='gray',style=PIESLICE)
shan_5=canvas.create_arc(30,30,270,270,extent=extent_list[4],start=start_list[4],fill='green',style=PIESLICE)
shan_6=canvas.create_arc(30,30,270,270,extent=extent_list[5],start=start_list[5],fill='pink',style=PIESLICE)
shan_7=canvas.create_arc(30,30,270,270,extent=extent_list[6],start=start_list[6],fill='black',style=PIESLICE)
shan_8=canvas.create_arc(30,30,270,270,extent=extent_list[7],start=start_list[7],fill='yellow',style=PIESLICE)
shan_9=canvas.create_arc(30,30,270,270,extent=extent_list[8],start=start_list[8],fill='red',style=PIESLICE)
canvas.grid(row=1,column=0)
为了指明是哪个选项被选择到,绘制一个小箭头:
jiantou=canvas.create_polygon(145,16,155,16,150,28,fill='red')
开始旋转的按钮设置:
button_choose=Button(win,text="选择",command=rotate_arc)
button_choose.grid(row=2,column=0)
编写旋转的方法:
这里的实现思路是利用canvas的删除+绘制,再加上create_arc方法的start参数。
先随机一个时间,为了保证几率平均,随机数的取值上下范围都是0.36的整倍数,然后每隔0.001秒,重新绘制,角度增加10度。(这里的0.001和10 都可以自己调整设置)
def rotate_arc():
fast_time=round(random.uniform(1.44,2.88),2)
begin_start=0
for i in range(int(fast_time*100)):
time.sleep(0.001)
canvas.delete(ALL)
jiantou = canvas.create_polygon(145, 16, 155, 16, 150, 28, fill='red')
shan_1 = canvas.create_arc(30, 30, 270, 270, extent=extent_list[0], start=start_list[0]-i*5, fill='blue', style=PIESLICE)
shan_2 = canvas.create_arc(30, 30, 270, 270, extent=extent_list[1], start=start_list[1]-i*5, fill='orange', style=PIESLICE)
shan_3 = canvas.create_arc(30, 30, 270, 270, extent=extent_list[2], start=start_list[2]-i*5, fill='white', style=PIESLICE)
shan_4 = canvas.create_arc(30, 30, 270, 270, extent=extent_list[3], start=start_list[3]-i*5, fill='gray', style=PIESLICE)
shan_5 = canvas.create_arc(30, 30, 270, 270, extent=extent_list[4], start=start_list[4]-i*5, fill='green', style=PIESLICE)
shan_6 = canvas.create_arc(30, 30, 270, 270, extent=extent_list[5], start=start_list[5]-i*5, fill='pink', style=PIESLICE)
shan_7 = canvas.create_arc(30, 30, 270, 270, extent=extent_list[6], start=start_list[6]-i*5, fill='black', style=PIESLICE)
shan_8 = canvas.create_arc(30, 30, 270, 270, extent=extent_list[7], start=start_list[7]-i*5, fill='yellow', style=PIESLICE)
shan_9 = canvas.create_arc(30, 30, 270, 270, extent=extent_list[8], start=start_list[8]-i*5, fill='red', style=PIESLICE)
begin_start=i*5
win.update()
begin_start = begin_start + 95
while begin_start > 360:
begin_start = begin_start - 360
for it in range(9):
if start_list[it]<begin_start:
chosen=color_list[it]
实现结果:
之后,就可以根据这个执行结果,继续配置其他功能。