mac中安裝wxpython

1、簡介


wxPython是Python語言的一套優秀的GUI圖形庫,容許Python程序員很方便的建立完整的、功能鍵全的GUI用戶界面。 wxPython是做爲優秀的跨平臺GUI庫wxWidgets的Python封裝和Python模塊的方式提供給用戶的。html

2、安裝


一、安裝python3.5.2

Python 3.5.2官方安裝包列表
選擇 Mac OS X 64-bit/32-bit installer 下載後,雙擊安裝。
安裝完成後,命令行下執行:python

➜  ~ 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 ,很是簡單,安裝完成。程序員

二、安裝wxpython

wxpython各個版本的安裝包快照列表
選擇 python3.5.2對應的安裝包wxPython_Phoenix-3.0.3.dev2700+c524ed1-cp35-cp35m-macosx_10_6_intel.whl
可經過瀏覽器下載,而後執行macos

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模塊app

➜  ~ 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>

表示該模塊已經安裝成功,而且能夠正常運行了。oop

3、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()

參考文檔

python學習筆記十四:wxPython Demo測試

相關文章
相關標籤/搜索