wxPython是Python语言的一套优秀的GUI图形库,允许Python程序员很方便的创建完整的、功能键全的GUI用户界面。 wxPython是作为优秀的跨平台GUI库wxWidgets的Python封装和Python模块的方式提供给用户的。
app =<span style="color: #000000;"> wx.App()
win =<span style="color: #000000;"> wx.Frame(
None,title=<span style="color: #800000;">"<span style="color: #800000;">simple editor<span style="color: #800000;">"<span style="color: #000000;">,size=(410,335<span style="color: #000000;">))
bkg =<span style="color: #000000;"> wx.Panel(win)
<span style="color: #0000ff;">def<span style="color: #000000;"> openFile(evt):
dlg =<span style="color: #000000;"> wx.FileDialog(
win,<span style="color: #800000;">"<span style="color: #800000;">Open<span style="color: #800000;">"<span style="color: #000000;">,<span style="color: #800000;">""<span style="color: #000000;">,<span style="color: #800000;">"<span style="color: #800000;">All files (.)|.<span style="color: #800000;">"<span style="color: #000000;">,wx.FD_OPEN |<span style="color: #000000;"> wx.FD_FILE_MUST_EXIST)
filepath = <span style="color: #800000;">''
<span style="color: #0000ff;">if dlg.ShowModal() ==<span style="color: #000000;"> wx.ID_OK:
filepath =<span style="color: #000000;"> dlg.GetPath()
<span style="color: #0000ff;">else<span style="color: #000000;">:
<span style="color: #0000ff;">return<span style="color: #000000;">
filename.SetValue(filepath)
fopen =<span style="color: #000000;"> open(filepath)
fcontent =<span style="color: #000000;"> fopen.read()
contents.SetValue(fcontent)
fopen.close()
<span style="color: #0000ff;">def<span style="color: #000000;"> saveFile(evt):
fcontent =<span style="color: #000000;"> contents.GetValue()
fopen = open(filename.GetValue(),<span style="color: #800000;">'<span style="color: #800000;">w<span style="color: #800000;">'<span style="color: #000000;">)
fopen.write(fcontent)
fopen.close()
openBtn = wx.Button(bkg,label=<span style="color: #800000;">'<span style="color: #800000;">open<span style="color: #800000;">'<span style="color: #000000;">)
openBtn.Bind(wx.EVT_BUTTON,openFile)
saveBtn = wx.Button(bkg,label=<span style="color: #800000;">'<span style="color: #800000;">save<span style="color: #800000;">'<span style="color: #000000;">)
saveBtn.Bind(wx.EVT_BUTTON,saveFile)
filename = wx.TextCtrl(bkg,style=<span style="color: #000000;">wx.TE_READONLY)
contents = wx.TextCtrl(bkg,style=<span style="color: #000000;">wx.TE_MULTILINE)
hbox =<span style="color: #000000;"> wx.BoxSizer()
hbox.Add(openBtn,proportion=0,flag=wx.LEFT | wx.ALL,border=5<span style="color: #000000;">)
hbox.Add(filename,proportion=1,flag=wx.EXPAND | wx.TOP | wx.BOTTOM,border=5<span style="color: #000000;">)
hbox.Add(saveBtn,border=5<span style="color: #000000;">)
bbox =<span style="color: #000000;"> wx.BoxSizer(wx.VERTICAL)
bbox.Add(hbox,flag=wx.EXPAND |<span style="color: #000000;"> wx.ALL)
bbox.Add(
contents,proportion=1<span style="color: #000000;">,flag=wx.EXPAND | wx.LEFT | wx.BOTTOM |<span style="color: #000000;"> wx.RIGHT,border=5<span style="color: #000000;">)
bkg.SetSizer(bbox)
win.Show()
app.MainLoop()