话不多说,先上代码
import random
import tkinter as tk
def shu():
t.insert(tk.INSERT,'很遗憾,你输了\n')
def ying():
t.insert(tk.INSERT, '恭喜你,赢了\n')
def ping():
t.insert(tk.INSERT, '平局\n')
def jiandao():
cpt = random.randint(0, 2)
t.delete(0.0, tk.END)
t.insert(tk.INSERT, '电脑为:'+data[cpt]+'\n')
if cpt == 0:
shu()
elif cpt==1:
ping()
else:ying()
def shitou():
cpt = random.randint(0, 2)
t.delete(0.0, tk.END)
t.insert(tk.INSERT, '电脑为:' + data[cpt] + '\n')
if cpt == 0:
ping()
elif cpt == 1:
ying()
else:
shu()
def bu():
cpt = random.randint(0, 2)
t.delete(0.0, tk.END)
t.insert(tk.INSERT, '电脑为:' + data[cpt] + '\n')
if cpt == 0:
ying()
elif cpt == 1:
shu()
else:
ping()
def qui():
win.destroy()
data = ['石头','剪刀','布']
win = tk.Tk()
win.geometry('500x500+300+300')
win.title("小说")
l = tk.Label(win, text='你好!欢迎来到猜拳游戏', bg='green', width=30, height=2).pack()
t = tk.Text(win, height=3)
t.pack()
b1 = tk.Button(win, text='剪刀', font=('Arial', 12), width=10, height=1, command=jiandao)
b1.pack()
b2 = tk.Button(win, text='石头', font=('Arial', 12), width=10, height=1, command=shitou)
b2.pack()
b3 = tk.Button(win, text='布', font=('Arial', 12), width=10, height=1, command=bu)
b3.pack()
b4 = tk.Button(win, text='退出', font=('Arial', 12), width=10, height=1, command=qui)
b4.pack()
win.mainloop()
if __name__ == "__main__":
pass
打包成exe程序
首先通过cmd 进入到你的py 文件位置
然后 pyinstaller -F -w game.py 打包程序 -F 打包成一个文件 -W 去掉黑框框
可以通过pip install pyinstaller 下载这个包
运行完成后
会在你的py文件目录生成三个文件夹, dist 文件夹下的exe 就是所要的
注意: 一定要有 if __name__ == "__main__":
pass
博主就是爬了1个多小时这个坑
https://pan.baidu.com/s/1CdHr4dF-9qXlGBHYc-Bcqw
提取码 u63e