用图形化界面先写一个小程序,界面如下图。
引用
# -*- coding: utf-8 -*-: import tkinter import threading import time
第一步,要有个界面,
#生成住窗口 大小 root = tkinter.Tk() #d定义图形界面标题 root.title('hello world') #窗口的大小 root.minsize(300,300)
第二步:按钮
#摆放按钮 btn1 = tkinter.Button(root,text = '中国',bg = 'red') btn1.place(x=20,y=20,width=50,height=50)
第三步:循环,选中的为红色,没选中的是灰色
#定义一个函数 循环我们的备选的选项的背景颜色 选中的俄日红色 没有被选中的为灰色 def round(): #作用域为全局作用域 global isloop global stopid #判断是否开启循环 if isloop ==True: return #从备选项开始 i = 1 #isinstance() 判断两个 if isinstance(stopid,int): i = stopid #开始循环 while True: #延时操作 time.sleep(0.03) #将所有的组建的背景颜色变为白色 for x in girllists: x['bg']='white' #将当前的数值变为红色 girllists[i]['bg']='red' i+=1 print('当前i为:',i) #ruguo i>最大的索引值 if i>=len(girllists): i = 0 if stopsign ==True: isloop = False stopid = i break
第四步:定义开始 停止
#定义停止的方法 def stop1(): global stopsign if stopsign == True: return stopsign =True #定义开始 def newtask(): global isloop global stopsign stopsign = False t = threading.Thread(target=round) #开启线程 t.start() #设置循环开启标志 isloop = True
第五步,开始,停止按钮
btn_start = tkinter.Button(root,text='开始',command=newtask) btn_start.place(x= 90,y=125,width=50,height=50) btn_stop=tkinter.Button(root,text='停止',command=stop1) btn_stop.place(x=160,y=125,width=50,height=50)
完整代码:
# -*- coding: utf-8 -*-: import tkinter import threading import time #生成住窗口 大小 root = tkinter.Tk() #d定义图形界面标题 root.title('hello world') #窗口的大小 root.minsize(300,300) #摆放按钮 btn1 = tkinter.Button(root,text = '中国',bg = 'red') btn1.place(x=20,y=20,width=50,height=50) btn2 =tkinter.Button(root,text = '日本',bg = 'white') btn2.place(x=90,y=20,width=50,height=50) btn3 = tkinter.Button(root,text = '美国',bg = 'white') btn3.place(x=160,y=20,width=50,height=50) btn4 = tkinter.Button(root,text = '俄罗斯',bg = 'white') btn4.place(x=230,y=20,width=50,height=50) btn5 = tkinter.Button(root,text = '德国',bg = 'white') btn5.place(x=230,y=90,width=50,height=50) btn6 = tkinter.Button(root,text = '英国',bg = 'white') btn6.place(x=230,y=160,width=50,height=50) btn7 =tkinter.Button(root,text = '法国',bg = 'white') btn7.place(x=230,y=230,width=50,height=50) btn8 = tkinter.Button(root,text = '韩国',bg = 'white') btn8.place(x=160,y=230,width=50,height=50) btn9 = tkinter.Button(root,text = '意大利',bg = 'white') btn9.place(x=90,y=230,width=50,height=50) btn10 =tkinter.Button(root,text = '加拿大',bg = 'white') btn10.place(x=20,y=230,width=50,height=50) btn11 = tkinter.Button(root,text = '印度',bg = 'white') btn11.place(x=20,y=160,width=50,height=50) btn12 = tkinter.Button(root,text = '澳大利亚',bg = 'white') btn12.place(x=20,y=90,width=50,height=50) #将多选选项添加到列表中 girllists = [btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12,] #定义一个标记 是否开启循环 isloop = False #是否停止 stopsign = False #储存挺值得id 目的 通过索引来确定对应的选项 stopid = None #定义一个函数 循环我们的备选的选项的背景颜色 选中的俄日红色 没有被选中的为灰色 def round(): #作用域为全局作用域 global isloop global stopid #判断是否开启循环 if isloop ==True: return #从备选项开始 i = 1 #isinstance() 判断两个 if isinstance(stopid,int): i = stopid #开始循环 while True: #延时操作 time.sleep(0.03) #将所有的组建的背景颜色变为白色 for x in girllists: x['bg']='white' #将当前的数值变为红色 girllists[i]['bg']='red' i+=1 print('当前i为:',i) #ruguo i>最大的索引值 if i>=len(girllists): i = 0 if stopsign ==True: isloop = False stopid = i break #定义停止的方法 def stop1(): global stopsign if stopsign == True: return stopsign =True #定义开始 def newtask(): global isloop global stopsign stopsign = False t = threading.Thread(target=round) #开启线程 t.start() #设置循环开启标志 isloop = True btn_start = tkinter.Button(root,text='开始',command=newtask) btn_start.place(x= 90,y=125,width=50,height=50) btn_stop=tkinter.Button(root,text='停止',command=stop1) btn_stop.place(x=160,y=125,width=50,height=50) root.mainloop()