以下是我根据英语老师的要求,制作的一个小程序,功能是从预先设定的图库中抽取一张让迟到的学生进行模仿
附上我自己做程序的一些坑,如有错误请联系我或直接指出:
1.我使用的是python3.6版本,在程序打包,用pyinstaller将.py 转成 .exe 文件时出现了打包失败的小问题,问题解决参考:点击打开链接
2.在调用button控件的command控制函数时,如果有变量进入,则必须保证变量为全局变量
3.import Image失败,可修改成 import PIL.Image;
同理,如果出现报错image无open属性的提示(type object 'Image' has no attribute 'open'),请将Image修改为PIL.Image.open
4.代码迭代的基例问题,不再多谈
5.lalelimage.configure(imgae = im)和 labelimage.image = im 必须同时使用,原理不明
6.做这个的时候参考了一位大佬的,大家可以去看看:点击打开链接
7.PhotoIamge仅支持gif格式的,最简单的人工方法改格式是:将图片用画图打开,另存为gif格式。
8.音乐文件处理具有多种形式,这里采用winsound库(自带),使用参考:http://blog.csdn.net/foreverxyz/article/details/6953907
9.一个很重要的部分: tk . update ( ) 使sleep函数不会干扰到程序运行下面是源码:(12-19更新)
# -*- coding: utf-8 -*- """ Created on Wed Dec 13 20:52:48 2017 @author: 29428 """ from random import * from PIL import Image, ImageTk import PIL.Image from tkinter import * import os import time import winsound list = [] count = [] def randnum(): fn = [] path = os.listdir('./picture') for files in path: name,end = os.path.splitext(files) fn.append(name) return len(fn),fn def check(maxnum): a = randint(1,maxnum-1) if a in list: if len(list) == maxnum-1: return None else: return check(maxnum) # 注意,这里有超过最大迭代深度的危险 else: list.append(a) return a return a '''因为最后一张图片写的是免除惩罚吗(笑)''' def processzero(): button0.configure(text = "停止",command=processzero2) winsound.PlaySound(r".\dll\music.wav",winsound.SND_FILENAME|winsound.SND_ASYNC) left = 5 while len(count) == 0: for e in fn: if len(count) != 0: left = left -1 if left != 0: im = PIL.Image.open(r".\\picture\\"+str(e)+".gif") im = im.resize((800,850)) photo = ImageTk.PhotoImage(im) lblimage.configure(image = photo) lblimage.image = photo tk.update() if e == str(maxnum): time.sleep(0.1) else: i = randint(1,50) i = 0.025 * i/10 time.sleep(i) else: break if len(count) != 0: b = str(check(maxnum)) im = PIL.Image.open(r".\\picture\\"+b+".gif") im = im.resize((800,850)) photo = ImageTk.PhotoImage(im) lblimage.configure(image = photo) lblimage.image = photo winsound.PlaySound("SystemExit", winsound.SND_ALIAS) def processzero2(): text1 = "恭喜您竟然获得了超稀有的SSR动作卡片:\n ~~寂寞的芙蓉姐姐哦(喵)~~" notice.configure(text = text1) count.append('stop') def processtwo(): os._exit(0) # windows中安全退出 def processone(): try: b = str(check(maxnum)) im = PIL.Image.open(r".\\picture\\"+b+".gif") im = im.resize((800,850)) photo = ImageTk.PhotoImage(im) lblimage.configure(image = photo) lblimage.image = photo except: print('您的选择次数太多啦!') processtwo() maxnum,fn = randnum() a = check(maxnum) tk = Tk() tk.title('动作抽卡器') screenwidth = str(tk.winfo_screenwidth()-100) screenheight = str(tk.winfo_screenheight() - 100) tk.geometry(screenwidth +'x'+ screenheight) #是x 不是* tk.resizable(width=False, height=False) #宽,高不可变, 默认为True text0 = "点击开始即可进行选择:\n ~还在等什么,赶快行动吧~" notice = Label(tk,text=text0,font=("FangSong",16)) notice.pack(side = "top") ls = [] try: fo = open("./dll/direction.txt","r") # print(fo,type(fo)) for line in fo: ls.append(line) except: print("文件缺失") word = str('\n'*7+ls[0])+'\n'+str(ls[1])+'\n'+str(ls[2])+'\n'+str(ls[3]) lblimage = Label(tk,text = word,justify='center',font =("YouYuan",25) ) # justify='center' 多行文本居中对齐 lblimage.pack() frm = Frame(tk) button0 = Button(frm,text="开始",width =50,command=processzero) button0.pack(side ="left") button1 = Button(frm,text="换一幅",width =50,command=processone) button1.pack(side ="left") button2 = Button(frm,text="结束",width = 50,command=processtwo) button2.pack(side = "right") frm.pack(side = "bottom") tk.mainloop()