一、簡介
wxPython是Python語言的一套優秀的GUI圖形庫,允許Python程式員很方便的建立完整的、功能鍵全的GUI使用者介面。 wxPython是作為優秀的跨平台GUI庫wxWidgets的Python封裝和Python模組的方式提供給使用者的。
二、安裝
參考官方網站:http://www.wxpython.org/download.php
三、DEMO
本demo是一個簡單的記事本軟體,可以開啟檔案,修改並儲存。
import wxapp = 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()
運行效果: