mac中安装wxpython

一、简介


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

参考文档

python学习笔记十四:wxPython Demo

posted on 2017-01-19 11:18  mingaixin  阅读(875)  评论(0编辑  收藏  举报