尝试完成一个大转盘,代码,思路很简单,大部分函数在上一次讲解计算器时使用过,这次主要是实现一个线程和函数绑定,就当做是练手吧。
线程的讲解:https://www.cnblogs.com/chengd/articles/7770898.html
Button小部件是一个标准的Tkinter小部件:
https://www.cnblogs.com/mathpro/p/8066530.html
详细解释下:
global #全局函数定义
from tkinter import *#引入tkinter,类比于java中的gui的swing
import threading#用于提供线程相关的操作,线程是应用程序中工作的最小单元。
Target#一般是一个可调用对象,比如函数,如果你想让一个变量作为这个参数,可以重写这个变量所在类的__callable__方法,让变量可调用。
题外话:
1.mainloop:就进入到事件(消息)循环。一旦检测到事件,就刷新组件。譬如你输入一个字符,就要立即在光标那个位置显示出来(前提是你选中了文本框,也就是鼠标在文本框这个图案的范围内单击过)。又譬如你点了首页这个按钮(就是在这个图形的区域附近单击)那么就要清除你浏览器里的全部部件,然后重新绘制(按照主页设计的布局和内容)。
2.__name__ == '__main__':当.py文件被直接运行时,if __name__ == '__main__'之下的代码块将被运行;当.py文件以模块形式被导入时,if __name__ == '__main__'之下的代码块不被运行。
帮助网页:
效果图:
附上代码:
import tkinter
import time
import threading#用于提供线程相关的操作,线程是应用程序中工作的最小单元。
#from PIL import Image
root = tkinter.Tk()
root.title('下一个谁讲')
root.minsize(450,350)
btn1 = tkinter.Button(text = '姚志立',bg = 'red')
btn1.place(x = 20, y = 20, width = 50, height = 50)
btn2 = tkinter.Button(text = '于涛',bg = 'white')
btn2.place(x = 90, y = 20, width = 50, height = 50)
btn3 = tkinter.Button(text = '李世楠',bg = 'white')
btn3.place(x = 160, y = 20, width = 50, height = 50)
btn4 = tkinter.Button(text = '罗豆豆',bg = 'white')
btn4.place(x = 230, y = 20, width = 50, height = 50)
btn5 = tkinter.Button(text = '刘宇',bg = 'white')
btn5.place(x = 230, y = 90, width = 50, height = 50)
btn6 = tkinter.Button(text = '于涛',bg = 'white')
btn6.place(x = 230, y = 160, width = 50, height = 50)
btn7 = tkinter.Button(text = '李思楠',bg = 'white')
btn7.place(x = 230, y = 230, width = 50, height = 50)
btn8 = tkinter.Button(text = '罗豆豆',bg = 'white')
btn8.place(x = 160, y = 230, width = 50, height = 50)
btn9 = tkinter.Button(text = '刘宇',bg = 'white')
btn9.place(x = 90, y = 230, width = 50, height = 50)
btn10 = tkinter.Button(text = '于涛',bg = 'white')
btn10.place(x = 20, y = 230, width = 50, height = 50)
btn11 = tkinter.Button(text = '李世楠',bg = 'white')
btn11.place(x = 20, y = 160, width = 50, height = 50)
btn12 = tkinter.Button(text = '刘宇',bg = 'white')
btn12.place(x = 20, y = 90, width = 50, height = 50)
'''button_img_gif = Tkinter.PhotoImage(file = 'button_gif.gif')
button_img = Tkinter.Button(root, image = button_img_gif, text = '带图按钮')
button_img.pack() #没能实现,不知道为什么,是个坑
'''
new=tkinter.Label(text="来看看在我后面哪个讲",bg='white')
new.place(x=300,y=20,width=150, height=50)
#将所有选项组成列表
carlist = [btn1,btn2,btn3,btn4,btn5,btn6,btn6,btn7,btn8,btn9,btn10,btn11,btn12]
#是否开始循环的标志
isloop = False
def round():
#判断是否开始循环
if isloop == True:
return#结束函数的执行,从函数返回
#初始化计数 变量
i = 0
#死循环
while True:
time.sleep(0.1)
#将所有的组件背景变为白色
for x in carlist:
x['bg'] = 'white'
#将当前数值对应的组件变色
carlist[i]['bg'] = 'red'
#变量+1
i += 1
#如果i大于最大索引直接归零
if i >= len(carlist):
i = 0
if functions ==True :
neww=tkinter.Label(text=" ",bg='white')
neww.place(x=300,y=100,width=150, height=50)
continue
else :
if i==1:
neww=tkinter.Label(text="我正在讲好不好",bg='white')
neww.place(x=300,y=100,width=150, height=50)
break
elif i>1 and i<=5:
neww=tkinter.Label(text="我是来看你们表演的",bg='white')
neww.place(x=300,y=100,width=150, height=50)
break
elif i>5 and i<=7:
neww=tkinter.Label(text="忽略我,请进行下一个",bg='white')
neww.place(x=300,y=100,width=150, height=50)
break
elif i>7:
neww=tkinter.Label(text="呵呵呵~",bg='white')
neww.place(x=300,y=100,width=150, height=50)
break
#建立一个新线程的函数。
def newtask():
global isloop#全局函数定义
global functions
#建立新线程
t = threading.Thread(target= round)#给函数传递回调对象
#开启线程运行
t.start()
#设置程序开始标志
isloop = True
#是否运行的标志
functions = True
#定义停止循环的函数
def stop():
global isloop
global functions
functions = False
isloop = False
#开始按钮
btn_start = tkinter.Button(root,text = 'start',command = newtask,bg='pink')
btn_start.place(x = 90, y = 120, width = 50, height = 50)
btn_stop = tkinter.Button(root,text = 'stop',command = stop,bg='green')
btn_stop.place(x = 160, y = 120, width = 50, height = 50)
root.mainloop()