首先要製作一個很普通的 win app 就像上面那個程式執行後的畫面一樣,也許在 Windows 下有很多開發工具可以很簡單就達到這樣的效果,但卻無法把程式拿到 Linux 或 MAC OSX 下執行,所以為了跨平台而選用 Python 而且 python 不只跨平台而且功能強大,Google 或 Dropbox 還有其他大公司和強大的網路系統都使用他。
如果要把 python 拿來開發一些 Windows 上使用者的簡單操作介面也是不難的,因為python本身內建了GUI庫TK,但 wxPython 更強大,連 GUI 介面設計工具都有。安裝後在你的Windows程式裡就會有這些工具跟範例,很容易上手。
儲存成 resource1.xrc 檔案
<?xml version="1.0" ?>
<resource>
<object class="wxFrame" name="FRAME1">
<title>測試窗口</title>
<icon>favicon.ico</icon>
<object class="wxPanel" name="MyPanel">
<object class="wxButton" name="CloseButton">
<label>關閉</label>
<pos>15,10</pos>
</object>
</object>
</object>
</resource>
接下來開始編輯 python 檔案,test2.py 如下
import wx
import wx.xrc as xrc
class MyApp(wx.App):
def OnInit(self):
self.res = xrc.XmlResource(r"./resource1.xrc")
self.frame_1 = self.res.LoadFrame(None, "FRAME1")
self.Bind(wx.EVT_BUTTON, self.OnClose, id=xrc.XRCID('CloseButton'))
self.frame_1.Show()
return 1
# End of OnInit().
def OnClose(self, event):
self.frame_1.Close()
# End of class MyApp.
if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()
解釋:
self.res = xrc.XmlResource(r"./resource1.xrc")
把你的GUI讀進來並綁定視窗
self.frame_1 = self.res.LoadFrame(None, "FRAME1")
接下來將按鈕事件把定到你的副程式
self.Bind(wx.EVT_BUTTON, self.OnClose, id=xrc.XRCID('CloseButton'))
基本上程式已經完成了,只是這個程式還得透過有安裝 python 跟 wxPython 的電腦才能執行,所以還得進行一些封裝動作讓這個程式可以COPY到任何 windows PC 上執行。
如果只是要在 Windows 下封裝成可執行檔,可以選用最多人使用的 py2exe 來製作。
from distutils.core import setup
import py2exe
setup(console=['你要編譯的檔案名稱.py'])
再下指令 python py2setup.py py2exe
他就會幫你產生一個dist目錄,裡面有個 test2.exe 就是封裝完成的執行檔,很快你會發現檔案並沒有正常執行,因為 resource1.xrc 跟 favicon.ico 並不存在,也就是說主要只是把你指定的 .py 檔編譯成 .exe 並把執行過程需要的檔案都收集到這個目錄下(並不包含其他你所開發或引用的檔案),你自己開發引用的檔案必須自己手動COPY到這個目錄下,然後就可以正常執行了。
如果當 py2exe 出現找不到某些模組時使用參數 python setup.py py2exe -p win32com -i twisted.web.resource 在 p後面接package名稱
但有個讓人討厭的地方就是程式啟動時總會帶一個 Consol 視窗。
依目前範例只要進行兩個動作就可以完成封裝。
python pyinstaller路徑\Makespec.py -F -w test2.py 產生 test2.spec
python pyinstaller路徑\Build.py test2.spec 產生 dist 目錄,把引用檔COPY過去後一樣就可以正常執行了,而且因為下了 -w 參數,所以執行過程不會再帶著一個 consol 視窗。那 -F 參數勒?有 -F 參數表示將程式編譯成單一執行檔。注意當 test2.spec 文字檔裡的相關路徑含有中文很容易出錯,所以可以暫時移到不使用中文的目錄下去製作spec或乾脆不要使用中文做目錄命名。
開發過程最好設一個專案目錄,此目錄下包含一個 src(開發的原始程式) , bin(執行檔) 跟其他一些目錄(一些XRC...或其他引用檔)。這樣的好處是在 src 下產生的 dist 所有檔案可以全部移到 bin 下便可以完整正常執行,要製作安裝檔或給別人使用時只要將 src 目錄移除便可以了。
Makespec.py 的參數如下,其他請到關網參考
-F, --onefile
produce a single file deployment (see below).
-D, --onedir
produce a single directory deployment (default).
-K, --tk
include TCL/TK in the deployment.
-a, --ascii
do not include encodings. The default (on Python versions with unicode support) is now to include all encodings.
-d, --debug
use debug (verbose) versions of the executables.
-w, --windowed, --noconsole
Use the Windows subsystem executable, which does not open the console when the program is launched. (Windows only)
-c, --nowindowed, --console
Use the console subsystem executable. This is the default. (Windows only)
-s, --strip
the executable and all shared libraries will be run through strip. Note that cygwin's strip tends to render normal Win32 dlls unusable.
-X, --upx
if you have UPX installed (detected by Configure), this will use it to compress your executable (and, on Windows, your dlls). See note below.
-o DIR, --out=DIR
create the spec file in directory. If not specified, and the current directory is Installer's root directory, an output subdirectory will be created. Otherwise the current directory is used.
-p DIR, --paths=DIR
set base path for import (like using PYTHONPATH). Multiple directories are allowed, separating them with the path separator (';' under Windows, ':' under Linux), or using this option multiple times.
--icon=<FILE.ICO>
add file.ico to the executable's resources. (Windows only)
--icon=<FILE.EXE,N>
add the n-th incon in file.exe to the executable's resources. (Windows only)
-v FILE, --version=FILE
add verfile as a version resource to the executable. (Windows only)
-n NAME, --name=NAME
optional name to assign to the project (from which the spec file name is generated). If omitted, the basename of the (first) script is used.
3 則留言:
您好,謝謝您的教學。
我用pyinstaller 打包以後的檔案,如果放到含有中文名稱的資料夾就無法執行,請問有沒有什麼方法?
您好,謝謝您的教學。請問我目前是安裝python3.4版,我google了一段時間找不到其對應的xypython版本,是目前還沒有支援到3.4版嗎?但是又聽有些人說python3和python2中python3有比較多的功能,想請問是要跳回到python2去使用xypython,還是有支援3.4版的,只是我找不到.我好想用GUI介面阿(打滾).
對於一個程式小菜鳥,不要要我用文字介面來寫(滿地滾~~~~)
張貼留言