本文共 1046 字,大约阅读时间需要 3 分钟。
确保机子安装有以下程序:
①Python:可以从官方网站()下载
下载快速通道(Python2.7.2 win32):
②Py2exe:可以从官方网站()下载
下载快速通道(py2exe-0.6.9.win32-py2.7):
控制台应用程序
① 将代码另存为.py文件(本例命名为test.py),并确保代码能够正确运行。
下面的python脚本代码是在控制台中输出数字0-9。
print "Python script to exe test program"count = 0while count < 10: print "count = " + str(count) +"\n" count = count + 1可以在控制台中键入python filename(Python的安装目录要加入到系统的PATH中),回车后可以看到运行结果。
② 创建安装脚本。
from distutils.core import setupimport py2exesetup(console=['test.py'])在命令行控制台中运行python setup.py py2exe,可以看到有以下信息。
这样就生成exe可执行文件,可以在子目录\dist下找到test.exe文件。运行test.exe结果如下:
GUI应用程序
① 创建一个名为gui.py的新文件,内容如下:
from Tkinter import *frmMain = Tk()label = Label(frmMain, text="Welcome to py2exe!")label.pack()frmMain.mainloop()
简单解释一下代码的含义:导入Tkinter包,创建一个主窗口,在该窗口中创建一个标签,该标签的文本内容是“Welcome to py2exe!”并把标签的大小设为适应文本内容大小。
在命令行下输入“python gui.py”运行效果如下:
② 创建安装脚本
from distutils.core import setupimport py2exesetup(console=['gui.py'])
同样,在命令行控制台中运行python setup.py py2exe,可以看到生成了两个文件夹(build和dist),可以在\dist目录中找到gui.exe文件。
本人也是初学者,有什么写得不对的,还望大虾指正。。。
转载请注明出处!