首页 > 代码库 > wxpython记

wxpython记

 1 import wx
 2 def load(event):
 3     file = open(filename.GetValue())
 4     contents.SetValue(file.read())
 5     file.close()
 6 
 7 def save(event):
 8     file = open(filename.GetValue(),w)
 9     file.write(contents.GetValue())
10     file.close()
11 
12 
13 app = wx.App()
14 win = wx.Frame(None,title=Simple Editor,size=(410,335))
15 
16 bkg = wx.Panel(win)
17 
18 loadButton = wx.Button(bkg,label=Open)
19 loadButton.Bind(wx.EVT_BUTTON,load)
20 
21 saveButton = wx.Button(bkg,label=Save)
22 saveButton.Bind(wx.EVT_BUTTON,save)
23 
24 filename = wx.TextCtrl(bkg)
25 contents = wx.TextCtrl(bkg,style=wx.TE_MULTILINE|wx.HSCROLL)
26 
27 hbox = wx.BoxSizer()
28 hbox.Add(filename,proportion=1,flag=wx.EXPAND)
29 hbox.Add(loadButton,proportion=0,flag=wx.LEFT,border=5)
30 hbox.Add(saveButton,proportion=0,flag=wx.LEFT,border=5)
31 
32 vbox = wx.BoxSizer(wx.VERTICAL)
33 vbox.Add(hbox,proportion=0,flag=wx.EXPAND|wx.ALL,border=5)
34 vbox.Add(contents,proportion=1,flag=wx.EXPAND|wx.LEFT|wx.BOTTOM|wx.RIGHT,border=5)
35 
36 bkg.SetSizer(vbox)
37 win.Show()
38 
39 app.MainLoop()

BoxSizer (尺寸器),默认水平,可以先建立水平的BoxSizer 然后将水平的合成竖直的BoxSizer。

panel是幕布,flag设置相应参数,proportion 设置1(根据窗口改变时获取所有全部额外空间)和0。

Button有默认大小,可以用Size设置。

bind函数可以绑定事件函数。

TE_MULTILINE设置多行文本区

HSCROLL设置水平滚动条

默认套路是:

app = wx.App()
win = wx.Frame(None,title=‘Simple Editor‘,size=(410,335))

bkg = wx.Panel(win)

#你想放的代码

bkg.SetSizer(vbox)
win.Show()

app.MainLoop()

wxpython记