python pyinstaller 打包 猜拳游戏exe

本文介绍了如何使用PyInstaller将Python的猜拳游戏打包成一个没有控制台窗口的单一EXE文件。首先,需要在命令行进入Python文件所在目录,然后运行`pyinstaller -F -w game.py`命令进行打包。如果缺少`if __name__ == "__main__"`部分,程序可能无法正常工作。完成打包后,dist文件夹内的EXE文件即为目标程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

话不多说,先上代码

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值