使用cx_freeze把python打包exe示例
需要使用到的文件wxapp.py, read_file.py, setup.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#file: wxapp.py
import wx
import os
import sys
import read_file
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent=None, title='Hello from cx_Freeze')
panel = wx.Panel(self)
closeMeButton = wx.Button(panel, -1, 'Close Me')
wx.EVT_BUTTON(self, closeMeButton.GetId(), self.OnCloseMe)
wx.EVT_CLOSE(self, self.OnCloseWindow)
pushMeButton = wx.Button(panel, -1, 'Push Me')
wx.EVT_BUTTON(self, pushMeButton.GetId(), self.OnPushMe)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(closeMeButton, flag=wx.ALL, border=20)
sizer.Add(pushMeButton, flag=wx.ALL, border=20)
panel.SetSizer(sizer)
topSizer = wx.BoxSizer(wx.VERTICAL)
topSizer.Add(panel, flag=wx.ALL | wx.EXPAND)
topSizer.Fit(self)
def OnCloseMe(self, event):
obj = read_file.PrintContent()
if getattr(sys, 'frozen', None):
path = os.path.dirname(sys.executable)
else:
path = os.path.dirname(__file__)
path = os.path.join(path, "read_file.py")
obj.show_content(path)
def OnPushMe(self, event):
wx.MessageBox('I was pushed!', 'Informational message')
def OnCloseWindow(self, event):
self.Destroy()
class App(wx.App):
def OnInit(self):
frame = Frame()
frame.Show(True)
self.SetTopWindow(frame)
return True
app = App(1)
app.MainLoop()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#file: read_file.py
class PrintContent(object):
def show_content(self, path):
f = open(path)
for line in f:
print line
f.close()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#file: setup.py
# A simple setup script to create an executable running wxPython. This also
# demonstrates the method for creating a Windows executable that does not have
# an associated console.
#
# wxapp.py is a very simple 'Hello, world' type wxPython application
#
# Run the build process by running the command 'python setup.py build'
#
# If everything works well you should find a subdirectory in the build
# subdirectory that contains the files needed to run the application
import sys
from cx_Freeze import setup, Executable
build_exe_options = {"optimize": 2,
"include_files": ["read_file.py"]}
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
executables = [Executable(script='wxapp.py',
base=base,
targetName="Demo.exe",
compress=True,
icon="py.ico")]
setup(name='wxapp',
version='0.1',
description='Sample cx_Freeze wxPython script',
options = {"build_exe": build_exe_options},
executables=executables)
打開cmd進(jìn)入代碼所在目錄,然后輸入:
python setup.py bdist_msi
然后會生成build和dist兩個文件夾,build文件夾里存放的是exe可執(zhí)行文件和所依賴的庫,直接把整個文件夾復(fù)制給別人就可以通過雙擊exe文件運行了,dist文件夾下是build文件夾的安裝程序,直接傳dist文件夾下的安裝包給朋友,朋友運行安裝包后會得到和build一樣的文件夾,路徑由用戶自己選擇
至于setup.py里面的參數(shù)選項可以自己去官網(wǎng)查看相應(yīng)的選項信息
- 一篇文章學(xué)會兩種將python打包成exe的方式
- Python打包為exe詳細(xì)教程
- Python打包成.exe可執(zhí)行文件的詳細(xì)步驟
- Python打包成exe文件的詳細(xì)操作指南
- 史上最詳細(xì)的Python打包成exe文件教程
- Python打包后的exe還原成.py的實現(xiàn)步驟
- 將python打包后的exe還原成py
- Python打包exe時各種異常處理方案總結(jié)
- Python打包代碼成exe可執(zhí)行文件的方法總結(jié)
- Python打包成exe常用的四種方法小結(jié)
- Python打包成exe的兩種方法
- python代碼打包到exe的實現(xiàn)示例
相關(guān)文章
Anaconda環(huán)境克隆、遷移的詳細(xì)步驟
最近需要在多臺計算機(jī)上工作,每次重新部署環(huán)境比較麻煩,所以學(xué)習(xí)一下anaconda環(huán)境遷移的方法,下面這篇文章主要給大家介紹了關(guān)于Anaconda環(huán)境克隆、遷移的詳細(xì)步驟,需要的朋友可以參考下2022-08-08
Pytorch-mlu?實現(xiàn)添加逐層算子方法詳解
本文主要分享了在寒武紀(jì)設(shè)備上?pytorch-mlu?中添加逐層算子的方法教程,代碼具有一定學(xué)習(xí)價值,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11
使用Python創(chuàng)建websocket服務(wù)端并給出不同客戶端的請求
本文主要介紹了使用Python創(chuàng)建websocket服務(wù)端并給出不同客戶端的請求,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

