引言
学习Python-GUI过程中接到一个需求(哈哈哈,来自老婆大人,刘老师的需求),学期末了,刘老师在短视频上刷到,有老师用ppt做一个抽红包的活动,给学生发奖励,也想着做一个,初版本使用ppt做了一个功能受限不太灵活,随即使用python实现个小应用得了。
功能结构
结构分为三个部分
- 小应用窗口-baseDesk
- 应用内初始界面-initFace,主要用来抽完一轮红包,再重新来一轮。每轮6个红包按钮,和一个开始按钮。
- 点击红包,切换界面-face1,主要是点击红包后,弹出红包后的奖励,即图片的切换,本文使用,随机数字获取奖励图片(图片名称末尾添加数字命名)。
先上图,小应用界面
抢红包啦
代码Demo
核心代码如下:
# coding=utf-8
import random,os
import tkinter as tk
base_path = os.getcwd()
class basedesk():
def __init__(self, master, photo):
self.root = master
self.root.title('抢红包啦')
self.root.geometry('320x200')
global hasNode
global startNums
hasNode = []
startNums = 0
initface(self.root, photo)
class initface():
def __init__(self, master, photo):
self.master = master
self.photo = photo
# # 基准界面initface
self.initface = tk.Frame(self.master, )
self.initface.pack()
button1 = tk.Button(self.initface, image=photo, cursor='hand2')
button2 = tk.Button(self.initface, image=photo, cursor='hand2')
button3 = tk.Button(self.initface, image=photo, cursor='hand2')
button4 = tk.Button(self.initface, image=photo, cursor='hand2')
button5 = tk.Button(self.initface, image=photo, cursor='hand2')
button6 = tk.Button(self.initface, image=photo, cursor='hand2')
button1.place(x=200,y=50)
button2.place(x=520,y=50)
button3.place(x=900,y=50)
button4.place(x=200,y=400)
button5.place(x=520,y=400)
button6.place(x=900,y=400)
btn = tk.Button(self.initface, text='开始',height = 10,width = 10,bg='red', command=self.change)
btn.place(x=10,y=250)
self.initface.place(rely=0, relheight=1, relwidth=1)
def change(self, ):
self.initface.destroy()
face1(self.master, self.photo)
class face1():
def __init__(self, master, photo):
self.master = master
self.photo = photo
self.photo1 = photo
self.face1 = tk.Frame(self.master, )
self.face1.pack()
# 构建 startNums 1-22 ,作为随机数字的范围,因为奖励图片命名为1-22 + 99,否则取不到图片便会报错
global startNums
if startNums <= 17:
startNums += 6
elif startNums >= 18 & startNums <= 22:
startNums += 4
# 开始抢红包构建
self.button1 = tk.Button(self.face1, image=photo, cursor='hand2', command=self.ranks_red_package1)
self.button2 = tk.Button(self.face1, image=photo, cursor='hand2', command=self.ranks_red_package2)
self.button3 = tk.Button(self.face1, image=photo, cursor='hand2', command=self.ranks_red_package3)
self.button4 = tk.Button(self.face1, image=photo, cursor='hand2', command=self.ranks_red_package4)
self.button5 = tk.Button(self.face1, image=photo, cursor='hand2', command=self.ranks_red_package5)
self.button6 = tk.Button(self.face1, image=photo, cursor='hand2', command=self.ranks_red_package6)
self.button1.place(x=200,y=50)
self.button2.place(x=520,y=50)
self.button3.place(x=900,y=50)
self.button4.place(x=200,y=400)
self.button5.place(x=520,y=400)
self.button6.place(x=900,y=400)
btn_back = tk.Button(self.face1, text='再来一波',height = 10,width = 10,bg='red', command=self.back)
btn_back.place(x=10,y=250)
self.face1.place(rely=0, relheight=1, relwidth=1)
def back(self):
self.face1.destroy()
initface(self.master, self.photo)
def ranks_opn_pic(self):
global end_nums
end_nums = 0
while end_nums != 99:
nums = random.randint(startNums - 5, startNums)
if nums not in hasNode:
break
elif len(hasNode) < 22:
nums = random.randint(startNums - 5,startNums)
else:
end_nums = 99
nums = 99
break
# print(hasNode)
# print(startNums)
hasNode.append(nums)
print("当前随机数为:{}".format(nums))
files_path = os.path.join(base_path,'redPackage/opedRed{}.png'.format(nums))
self.photo1 = tk.PhotoImage(file=files_path)
def ranks_red_package1(self):
self.ranks_opn_pic()
self.button1['image'] = self.photo1
def ranks_red_package2(self):
self.ranks_opn_pic()
self.button2['image'] = self.photo1
def ranks_red_package3(self):
self.ranks_opn_pic()
self.button3['image'] = self.photo1
def ranks_red_package4(self):
self.ranks_opn_pic()
self.button4['image'] = self.photo1
def ranks_red_package5(self):
self.ranks_opn_pic()
self.button5['image'] = self.photo1
def ranks_red_package6(self):
self.ranks_opn_pic()
self.button6['image'] = self.photo1
if __name__ == '__main__':
root = tk.Tk()
file_path = os.path.join(base_path,'redPackage/redPackage.png')
photo = tk.PhotoImage(file=file_path)
basedesk(root, photo)
root.mainloop()
打包成可运行应用(windows - exe为例)
- pyinstaller打包应用为exe
安装手动pip install pyinstaller - 打包使用以及对应参数说明
pyinstaller -F redPackageApp.py
常用可选项及说明:
- -F:打包后只生成单个exe格式文件;
- -D:默认选项,创建一个目录,包含exe文件以及大量依赖文件;
- -c:默认选项,使用控制台(就是类似cmd的黑框);
- -w:不使用控制台;
- -p:添加搜索路径,让其找到对应的库;
- -i:改变生成程序的icon图标。
- 注意点
- 在启动程序同一级目录,创建redPackage目录,将奖励图片放到该目录下,才可正常运行。
- 奖励图片命名:opedRed+ 数字.png
- 初始界面图片命名:opedRed.png (无需数字结尾)
网友评论