【Python】手把手教你撸个桌面应用~~.exe(Tkinte)

    xiaoxiao2025-07-27  28

    走流程先日常跪舔一波Python, 没有你想不到的,只有你做不到的!Pyhton作为年度翘楚,完美的诠释了这句话

    业务需求

    给定参数取出压缩文件夹中指定的文件并生成至目录,先上个效果图(原谅UI,以结果为主),不对号的小伙伴的欢迎绕道

    开发环境

    tkinter: python内置标准 Tk GUI 工具包的接口,免安装 参考文档: 1. https://www.runoob.com/python/python-gui-tkinter.html 菜鸟开心入门 2. https://cloud.tencent.com/developer/section/1372347 腾讯云爬坑进阶 3. https://docs.python.org/zh-cn/3/library/tkinter.html#tkinter-modules 官方文档劝你放弃(需翻墙*翻译未完成) 预留坑位:tkinter grid,pack布局,推荐grid,两者不可混合,具体优劣可自行百度 预留坑位:引入Combobox组件时需单独导, 不然回报Unresolved reference'Combobox'引入错误,正确姿势如下 预留坑位:Python3.x 版本使用的库名为 tkinter,即首写字母 T 为小写。 from tkinter import * from tkinter import ttk ttk.Combobox()

    PyInstaller: 打包python文件成exe格式 pywin32: windows拓展, PyInstaller依赖项

    pip install PyInstaller pip install pywin32

    程序构建

    又到了紧张又刺激的环节,输出你的“hello world "  =。=,以下代码来自《Python核心编程》——GUI章节的问候  

    import tkinter top = tkinter.Tk() # 获取顶层对象 # 装载文本标签 hello = tkinter.Label(top, text="hello world!") hello.pack() # 装载按钮,并注册退出事件 quit = tkinter.Button(top, text='QUIT', command=top.quit, bg='red', fg="white") quit.pack(fill=tkinter.X, expand=1) # 运行GUI应用 tkinter.mainloop()

    你看那妖艳的红配上那五彩斑斓的黑,原谅我一生放荡不羁爱自由,效果图如下

    打包,需CD到项目目录,小提示按住shift右击空白处,可在当前目录下打开cmd

    pyinstaller -F ***.py // *** 你的启动文件

    预留坑位: 打包失败!IndexError: tuple index out of range, pyinstaller 官方稳定版3.4, >3.4以上,需单独替换开发版构建文件,路径\venv\Lib\site-packages\PyInstaller\depend

    一切正常输出successfully即可完成打包

     

    在dist文件下即可查找到.exe可执行文件、目录结构变动如下

    预留坑位: 如报未找到依赖项时,需要把相关文件放到exe通级目录下,或者添加到\venv\Scripts 目录下  

    呼,搞完收工,洗洗睡~~

    最新回复(0)