Python实验⼗(tkinter编程实例)课件程序
1.使⽤tkinter实现计算器程序。实现效果如下
import re
import tkinter
ssagebox
root = tkinter.Tk()
#设置窗⼝⼤⼩和位置
#不允许改变窗⼝的⼤⼩
root.title('Simple Calculator')
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 = ()
if content.startswith('.'):
content ='0'+ content
if btn in'123456789':
content += btn
elif btn =='.':
#如果最后⼀个运算数中已经有⼩数点,就提⽰错误
lastPart = re.split(r'\+|-|\*|/]',content)[-1]
if'.'in lastPart:
return
else:
content += btn
elif btn =='C':
#清除整个表达式
content =''
elif btn =='=':
try:
#对输⼊的表达式求值
content =str(eval(content))
except:
return
elif btn in operators:
dswith(operators):
return
content += btn
elif btn =='Sqrt':
n = content.split('.')
if all(map(lambda x:x.isdigit(),n)):
content =eval(content)**0.5
else:
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)
#放置10个数字、⼩数点和平⽅根的按钮
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()
2.安装pillow和qrcode库并编写程序:⽣成带有图标的⼆维码,图标为⾃⼰设置的照⽚,扫描后打开某个⽹站。
from PIL import Image
code = qrcode.QRCode(
version =5,
error_correction = stants.ERROR_CORRECT_H,
box_size =10,
border =4,)
data ="www.baidu"
code.add_data(data=data)
code.make(fit=True)
code_img = code.make_image(fill_color="#ff3300", back_color="white")
code_size = code_img.size
code_size_w ,code_size_h = code_size
s =4
icon = Image.open(r'E:\girl.png')
icon_w,icon_h =int(code_size_w/s),int(code_size_h/s)
icon_small = size((icon_w ,icon_h),Image.ANTIALIAS)
icon_x, icon_y =int((code_size_w-icon_w)/2),int((code_size_h-icon_h)/2)
code_img.paste( icon_small,(icon_x,icon_y))
code_img.show()
code_img.save("girl_code.jpg")
3.使⽤tkinter实现抽奖式提问程序。给出⼈抽奖⼈员名单,['张三', '李四', '王五', '赵六', '周七', '钱⼋'],实现如下效果,点击开始,滚动姓名,开始抽奖,点击停,弹出中奖姓名。
ssagebox
import random
import threading
import itertools
import time
root = tkinter.Tk()
root.title("随机提问")
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 = le(t)
while root.flag:
lbFirst['text']= lbSecond['text']
lbSecond['text']= lbThird['text']
lbThird['text']=next(t)
time.sleep(0.1)
python新手编程100例def btnStartClick():
t = threading.Thread(target = switch)
t.start()
btnStart['state']='disabled'
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)
'本次中奖:'+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()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论