实例6(抽奖提问程序)
import tkinter
import tkinter.messagebox
import random
import threading
import itertools
import time
root=tkinter.Tk()
root.title('随机提问')
root.geometry('260x180+400+300')
root.resizable(False,False)
def closeWindow():
root.flag=False
time.sleep(0.1)
root.destroy()
root.protocol('WM_DELETE_WINDOW',closeWindow)
students=['张三','李四','王五','赵六','周七','钱八']
root.flag=False
def switch():
root.flag=True
t=students[:]
random.shuffle(t)
t=itertools.cycle(t)
while root.flag:
lbFirst['text']=lbSecond['text']
lbSecond['text']=lbThird['text']
lbThird['text']=next(t)
time.sleep(0.1)
def btnStartClick():
t=threading.Thread(target=switch)
t.start()
btnStart['state']='disable'
btnStop['state']='normal'
btnStart=tkinter.Button(root,text='开始',command=btnStartClick)
btnStart.place(x=30,y=10,width=80,height=20)
def btnStopClick():
root.flag=False
time.sleep(0.3)
tkinter.messagebox.showerror('恭喜','本次中奖:'+lbSecond['text'])
btnStart['state']='normal'
btnStop['state']='disabled'
btnStop=tkinter.Button(root,text='停',command=btnStopClick)
btnStop['state']='disabled'
btnStop.place(x=150,y=10,width=80,height=20)
lbFirst=tkinter.Label(root,text='')
lbFirst.place(x=80,y=60,width=100,height=20)
lbSecond=tkinter.Label(root,text='')
lbSecond['fg']='red'
lbSecond.place(x=80,y=90,width=100,height=20)
lbThird=tkinter.Label(root,text='')
lbThird.place(x=80,y=120,width=100,height=20)
root.mainloop()
效果图如下:
实例7(简易计算器程序)
import re
import tkinter
import tkinter.messagebox
root=tkinter.Tk()
root.geometry('300x270+400+100')
root.resizable(False,False)
root.title('简易计算器')
contentVar=tkinter.StringVar(root,'')
contentEntry=tkinter.Entry(root,textvariable=contentVar)
contentEntry['state']='readonly'
contentEntry.place(x=10,y=10,width=280,height=20)
def buttonClick(btn):
content=contentVar.get()
if content.startswith('.'):
content='0'+content
if btn in'0123456789':
content+=btn
elif btn=='.':
lastPart=re.split(r'\+|-|\*|/]',content)[-1]
if '.' in lastPart:
tkinter.messagebox.showerror('错误','小数点太多了')
return
else:
content+=btn
elif btn=='C':
content=''
elif btn=='=':
try:
content=str(eval(content))
except:
tkinter.messagebox.showerror('错误','表达式错误')
return
elif btn in operators:
if content.endswith(operators):
tkinter.messagebox.showerror('错误','不允许存在连续运算符')
return
content+=btn
elif btn=='Sqrt':
n=content.split('.')
if all(map(lambda x:x.isdigit(),n)):
content=eval(content)**0.5
else:
tkinter.messagebox.showerror('错误','表达式错误')
return
contentVar.set(content)
btnClear=tkinter.Button(root,text='Clear',command=lambda:buttonClick('C'))
btnClear.place(x=40,y=40,width=80,height=20)
btnCompute=tkinter.Button(root,text='=',command=lambda :buttonClick('='))
btnCompute.place(x=170,y=40,width=80,height=20)
digits=list('0123456789.')+['Sqrt']
index=0
for row in range(4):
for col in range(3):
d=digits[index]
index+=1
btnDigit=tkinter.Button(root,text=d,command=lambda x=d:buttonClick(x))
btnDigit.place(x=20+col*70,y=80+row*50,width=50,height=20)
operators=('+','-','*','/','**','//')
for index,operator in enumerate(operators):
btnOperator=tkinter.Button(root,text=operator,command=lambda x=operator:buttonClick(x))
btnOperator.place(x=230,y=80+index*30,width=50,height=20)
root.mainloop()
效果图如下:
实例9(定时自动关闭的窗口)
import time
import tkinter
import threading
root=tkinter.Tk()
root.title('倒计时自动关闭的窗口')
root['width']=400
root['height']=300
root.resizable(False,False)
richText=tkinter.Text(root,width=380)
richText.place(x=10,y=10,width=380,height=230)
richText.insert('0.0','假设阅读这些文字需要10秒钟时间')
lbTime=tkinter.Label(root,fg='red',anchor='w')
lbTime.place(x=10,y=250,width=150)
def autoClose():
for i in range(10):
lbTime['text']='距离窗口关闭还有{}秒'.format(10-i)
time.sleep(1)
root.destroy()
t=threading.Thread(target=autoClose)
t.start()
root.mainloop()
效果图如下: