Python 编译 exe 可执行程序
将Python文件编译为exe可执行程序1. 编写计算器源码2. 安装PyInstaller3. 用 PyInstaller 生成可执行程序4. 设置打包后的版本信息5. 编译.py文件为.exe可执行文件(有版本配置文件)6. 执行.exe文件隐藏cmd窗口Python程序py格式文件的优点是可以跨平台但运行必须有Python环境没有Python环境无法运行py格式文件。有没有方法用户不同安装Python就可直接运行开发的项目工程答案是肯定的。这就涉及到需要将Python的.py格式文件编写的脚本编译成一个系统可执行文件这可用PyInstaller来实现。PyInstaller支持在在Windows/Linux/Mac环境下将Python脚本打包成可执行程序在没有Python环境的机器上运行。注意需要在哪个操作系统平台一运行需在相应的操作系统(或虚拟机)下编译。1. 编写计算器源码import tkinter as tk root tk.Tk()root.title(计算器)root.geometry(295x280100100)font (宋体,20)font_16 (宋体,16)root.attributes(-alpha,0.9)result_num tk.StringVar()result_num.set()tk.Label(root,textvariableresult_num,fontfont,height2,width20,justifytk.LEFT,anchortk.SE).grid(row1,column1,columnspan4)button_clear tk.Button(root,textC,width5,fontfont_16,relieftk.FLAT,bg#b1b2b2)button_back tk.Button(root,text←,width5,fontfont_16,relieftk.FLAT,bg#b1b2b2)button_division tk.Button(root,text÷,width5,fontfont_16,relieftk.FLAT,bg#b1b2b2)button_multiplication tk.Button(root,text×,width5,fontfont_16,relieftk.FLAT,bg#b1b2b2)button_clear.grid(row2,column1,padx4,pady2)button_back.grid(row2,column2,padx4,pady2)button_division.grid(row2,column3,padx4,pady2)button_multiplication.grid(row2,column4,padx4,pady2)button_seven tk.Button(root,text7,width5,fontfont_16,relieftk.FLAT,bg#eacda1)button_eight tk.Button(root,text8,width5,fontfont_16,relieftk.FLAT,bg#eacda1)button_nine tk.Button(root,text9,width5,fontfont_16,relieftk.FLAT,bg#eacda1)button_subtraction tk.Button(root,text-,width5,fontfont_16,relieftk.FLAT,bg#b1b2b2)button_seven.grid(row3,column1,padx4,pady2)button_eight.grid(row3,column2,padx4,pady2)button_nine.grid(row3,column3,padx4,pady2)button_subtraction.grid(row3,column4,padx4,pady2)button_four tk.Button(root,text4,width5,fontfont_16,relieftk.FLAT,bg#eacda1)button_five tk.Button(root,text5,width5,fontfont_16,relieftk.FLAT,bg#eacda1)button_six tk.Button(root,text6,width5,fontfont_16,relieftk.FLAT,bg#eacda1)button_addition tk.Button(root,text,width5,fontfont_16,relieftk.FLAT,bg#b1b2b2)button_four.grid(row4,column1,padx4,pady2)button_five.grid(row4,column2,padx4,pady2)button_six.grid(row4,column3,padx4,pady2)button_addition.grid(row4,column4,padx4,pady2)button_one tk.Button(root,text1,width5,fontfont_16,relieftk.FLAT,bg#eacda1)button_two tk.Button(root,text2,width5,fontfont_16,relieftk.FLAT,bg#eacda1)button_three tk.Button(root,text3,width5,fontfont_16,relieftk.FLAT,bg#eacda1)button_equal tk.Button(root,text,width5,fontfont_16,relieftk.FLAT,bg#b1b2b2)button_one.grid(row5,column1,padx4,pady2)button_two.grid(row5,column2,padx4,pady2)button_three.grid(row5,column3,padx4,pady2)button_equal.grid(row5,column4,padx4,pady2)button_zero1 tk.Button(root,text ,width5,fontfont_16,relieftk.FLAT,bg#eacda1)button_zero tk.Button(root,text0,width5,fontfont_16,relieftk.FLAT,bg#eacda1)button_dot tk.Button(root,text.,width5,fontfont_16,relieftk.FLAT,bg#eacda1)button_equal2 tk.Button(root,text ,width5,fontfont_16,relieftk.FLAT,bg#b1b2b2)button_zero1.grid(row6,column1,padx4,pady2)button_zero.grid(row6,column2,padx4,pady2)button_dot.grid(row6,column3,padx4,pady2)button_equal2.grid(row6,column4,padx4,pady2)def click_button(x): print(X:\t,x)result_num.set(result_num.get()x)def calculation(): opt_str result_num.get()result eval(opt_str)result_num.set(str(result))def btnClearDisplay(): opt_str result_num.get()result eval(str(opt_str))result_num.set()button_one.config(commandlambda: click_button(1))button_two.config(commandlambda: click_button(2))button_three.config(commandlambda: click_button(3))button_four.config(commandlambda: click_button(4))button_five.config(commandlambda: click_button(5))button_six.config(commandlambda: click_button(6))button_seven.config(commandlambda: click_button(7))button_eight.config(commandlambda: click_button(8))button_nine.config(commandlambda: click_button(9))button_zero.config(commandlambda: click_button(0))button_dot.config(commandlambda: click_button(.))button_addition.config(commandlambda: click_button())button_subtraction.config(commandlambda: click_button(-))button_multiplication.config(commandlambda: click_button(*))button_division.config(commandlambda: click_button(/))button_clear.config(commandlambda: btnClearDisplay())button_equal.config(commandcalculation)root.mainloop()2. 安装PyInstallerpip install pyinstaller3. 用 PyInstaller 生成可执行程序PyInstaller工具的命令语法如下pyinstaller 选项 Python源文件不管这个 Python 应用是单文件的应用还是多文件的应用只要在使用 pyinstaller 命令时编译作为程序入口的 Python 程序即可。PyInstaller工具是跨平台的它既可以在 Windows平台上使用也可以在 Mac OS X 平台上运行。在不同的平台上使用 PyInstaller 工具的方法是一样的它们支持的选项也是一样的。先创建一个文件夹(目录)在该目录下创建一个.py文件(或复制一个.py文件)。然后转命令行窗口(cmb)进入到创建的文件夹(目录)下执行如下命令pyinstaller -F xxx.py执行上面命令将看到详细的生成过程。当生成完成后将会在当前目录下生成一个dist目录并在该目录下看到有一个xxx.exe文件这就是使用PyInstaller工具生成的exe程序。在上面命令中使用了-F选项该选项指定生成单独的exe文件因此在dist目录下生成了一个单独的xxx.exe文件(在Mac OS X平台上生成的文件没有后缀)与-F选项对应的是-D选项(默认选项)该选项指定生成一个目录(包含多个文件)来作为程序。在表1中列出的只是PyInstaller模块所支持的常用选项如果需要了解PyInstaller选项的详细信息则可通过pyinstaller -h来查看4. 设置打包后的版本信息编辑“版本信息文件”此文件为纯文本文件可用.txt扩展名文件名可随意如“file_version_info.txt”。典型的版本信息文件内容如下中文注释是作者为方便读者学习而加的注意红框中的项目。说明1.有关固定文件信息“ffi”的更多详细信息请参阅相关文献2. Translation 中的语言代码Locale ID信息见表2。5. 编译.py文件为.exe可执行文件(有版本配置文件)编辑“版本信息文件”文件名随意但需是文件文件如“myVerInfo.txt”内容如下VSVersionInfo(ffiFixedFileInfo(filevers(1,0,0,23),prodvers(1,0,0,1),mask0x3f,flags0x0,OS0x4,fileType0x1,subtype0x0,date(0,0)),kids[StringFileInfo([StringTable(080403a8,[StringStruct(CompanyName,解东),StringStruct(FileDescription,计算器),StringStruct(FileVersion,1.001),StringStruct(InternalName,计算器.exe),StringStruct(LegalCopyright,解东版权所有),StringStruct(OriginalFilename,test.py),StringStruct(ProductName,Python计算器),StringStruct(ProductVersion,1.005)])]),VarFileInfo([VarStruct(Translation,[2052,936])])])在该目录下执行如下命令pyinstaller -F --version-filemyVerInfo.txt test.py6. 执行.exe文件隐藏cmd窗口使用 PyInstaller 的 --windowed 或 --noconsole 选项pyinstaller -F --windowed --version-filemyVerInfo.txt test.py
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2547643.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!