一、簡介
wxPython是Python語言的一套優秀的GUI圖形庫,允許Python程式設計師很方便的建立完整的、功能鍵全的GUI使用者介面。 wxPython是作為優秀的跨平臺GUI庫wxWidgets的Python封裝和Python模組的方式提供給使用者的。
二、安裝
1、安裝python3.5.2
Python 3.5.2官方安裝包列表
選擇 Mac OS X 64-bit/32-bit installer 下載後,雙擊安裝。
安裝完成後,命令列下執行:
➜ ~ python3
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
OK ,非常簡單,安裝完成。
2、安裝wxpython
wxpython各個版本的安裝包快照列表
選擇 python3.5.2對應的安裝包wxPython_Phoenix-3.0.3.dev2700+c524ed1-cp35-cp35m-macosx_10_6_intel.whl
可通過瀏覽器下載,然後執行
pip3 install wxPython_Phoenix-3.0.3.dev2700+c524ed1-cp35-cp35m-macosx_10_6_intel.whl
或者
pip3 install https://wxpython.org/Phoenix/snapshot-builds/wxPython_Phoenix-3.0.3.dev2700+c524ed1-cp35-cp35m-macosx_10_6_intel.whl
這兩種安裝方式一樣,安裝完成之後進行測試wxpython模組
➜ ~ python3
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
➜ ~ import wx
➜ ~ wx.App()
<wx.core.App object at 0x1021e71f8>
表示該模組已經安裝成功,並且可以正常執行了。
三、DEMO
本demo是一個簡單的記事本軟體,可以開啟檔案,修改並儲存。
import wx
app = wx.App()
win = wx.Frame(
None,
title="simple editor",
size=(410, 335))
bkg = wx.Panel(win)
def openFile(evt):
dlg = wx.FileDialog(
win,
"Open",
"",
"",
"All files (*.*)|*.*",
wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
filepath = ''
if dlg.ShowModal() == wx.ID_OK:
filepath = dlg.GetPath()
else:
return
filename.SetValue(filepath)
fopen = open(filepath)
fcontent = fopen.read()
contents.SetValue(fcontent)
fopen.close()
def saveFile(evt):
fcontent = contents.GetValue()
fopen = open(filename.GetValue(), 'w')
fopen.write(fcontent)
fopen.close()
openBtn = wx.Button(bkg, label='open')
openBtn.Bind(wx.EVT_BUTTON, openFile)
saveBtn = wx.Button(bkg, label='save')
saveBtn.Bind(wx.EVT_BUTTON, saveFile)
filename = wx.TextCtrl(bkg, style=wx.TE_READONLY)
contents = wx.TextCtrl(bkg, style=wx.TE_MULTILINE)
hbox = wx.BoxSizer()
hbox.Add(openBtn, proportion=0, flag=wx.LEFT | wx.ALL, border=5)
hbox.Add(filename, proportion=1, flag=wx.EXPAND | wx.TOP | wx.BOTTOM, border=5)
hbox.Add(saveBtn, proportion=0, flag=wx.LEFT | wx.ALL, border=5)
bbox = wx.BoxSizer(wx.VERTICAL)
bbox.Add(hbox, proportion=0, flag=wx.EXPAND | wx.ALL)
bbox.Add(
contents,
proportion=1,
flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT,
border=5)
bkg.SetSizer(bbox)
win.Show()
app.MainLoop()