(一)wx.Frame背景色修正
import wx
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, style=wx.DEFAULT_FRAME_STYLE)
self.SetBackgroundColour(wx.NullColour)
class App(wx.App):
def OnInit(self):
frame = MainFrame()
frame.Show()
return True
if __name__ == '__main__':
app = App()
app.MainLoop()
(二)常用界面布局
wxt.py
#!/usr/bin/env python
import datetime
import wx
def ToPyDateTime(wxdate):
return datetime(wxdate.GetYear(), wxdate.GetMonth()+1, wxdate.GetDay())
def StaticBoxSizer(orient, parent, label=''):
box = wx.StaticBox(parent, label=label)
sizer = wx.StaticBoxSizer(box, orient)
return sizer
def StaticTextSizer(orient, parent, label='', widget = None):
box = wx.BoxSizer(orient)
if orient == wx.HORIZONTAL:
box.Add(wx.StaticText(parent, -1, label), 0, wx.ALIGN_CENTER|wx.ALL, 5)
box.Add(widget, 1, wx.ALIGN_CENTER|wx.ALL, 5)
elif orient == wx.VERTICAL:
box.Add(wx.StaticText(parent, -1, label), 0, wx.TOP|wx.RIGHT|wx.LEFT, 5)
box.Add(widget, 0, wx.EXPAND|wx.RIGHT|wx.LEFT|wx.BOTTOM, 5)
return box
test.py
import wx
import wxt
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, style=wx.DEFAULT_FRAME_STYLE)
self.SetBackgroundColour(wx.NullColour)
self.buttons = [ wx.Button(self, -1, str(i)) for i in range(4) ]
self.textCtrls = [ wx.TextCtrl(self) for i in range(4) ]
vbox = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(vbox)
#self.SetAutoLayout(True)
hbox0 = wxt.StaticBoxSizer(wx.HORIZONTAL, self, 'Middle Spacer')
hbox0.Add(self.buttons[0], 0, wx.ALL, 5)
hbox0.AddStretchSpacer()
hbox0.Add(self.buttons[1], 0, wx.ALL, 5)
hbox0.Add(self.buttons[2], 0, wx.ALL, 5)
hbox1 = wxt.StaticBoxSizer(wx.VERTICAL, self, 'Left Spacer')
hbox10 = wxt.StaticTextSizer(wx.HORIZONTAL, self, 'Text 0:', self.textCtrls[0])
hbox11 = wxt.StaticTextSizer(wx.HORIZONTAL, self, 'Text 1:', self.textCtrls[1])
hbox12 = wxt.StaticTextSizer(wx.VERTICAL, self, 'Text 2:', self.textCtrls[2])
hbox13 = wxt.StaticTextSizer(wx.VERTICAL, self, 'Text 3:', self.textCtrls[3])
hbox14 = wx.BoxSizer(wx.HORIZONTAL)
hbox14.AddStretchSpacer()
hbox14.Add(self.buttons[3], 0, wx.ALL, 5)
hbox1.Add(hbox10, 0, wx.EXPAND)
hbox1.Add(hbox11, 0, wx.EXPAND)
hbox1.Add(hbox12, 0, wx.EXPAND)
hbox1.Add(hbox13, 0, wx.EXPAND)
hbox1.Add(hbox14, 0, wx.EXPAND)
vbox.Add(hbox0, 0, wx.EXPAND|wx.ALL, 5)
vbox.Add(hbox1, 0, wx.EXPAND|wx.ALL, 5)
self.Fit()
class App(wx.App):
def OnInit(self):
frame = MainFrame()
frame.Show()
return True
if __name__ == '__main__':
app = App()
app.MainLoop()
(三)常用界面布局,简版
#!/usr/bin/env python
import datetime
import wx
def ToPyDateTime(wxdate):
return datetime.datetime(wxdate.GetYear(), wxdate.GetMonth()+1, wxdate.GetDay())
def BoxLayout(rootWidget = None, \
node = [], \
parent = None, \
parentOrient = wx.HORIZONTAL, \
gap = 5):
if type(node) != list:
node = [node]
me = None
orient = wx.HORIZONTAL
mask = 'a'
children = []
label = ''
if node[0] == 'hbox':
orient = wx.HORIZONTAL
me = wx.BoxSizer(orient)
mask = node[1]
children = node[2]
elif node[0] == 'vbox':
orient = wx.VERTICAL
me = wx.BoxSizer(orient)
mask = node[1]
children = node[2]
elif node[0] == 'hgroup':
orient = wx.HORIZONTAL
mask = node[1]
label = node[2]
children = node[3]
box = wx.StaticBox(rootWidget, label=label)
me = wx.StaticBoxSizer(box, orient)
elif node[0] == 'vgroup':
orient = wx.VERTICAL
mask = node[1]
label = node[2]
children = node[3]
box = wx.StaticBox(rootWidget, label=label)
me = wx.StaticBoxSizer(box, orient)
elif node[0] == 'hlabel':
orient = wx.HORIZONTAL
me = wx.BoxSizer(orient)
mask = node[1]
label = node[2]
child = node[3]
me.Add(wx.StaticText(rootWidget, -1, label), 0, wx.LEFT|wx.RIGHT|wx.ALIGN_CENTER, 5)
me.Add(child, 1, wx.ALL, 5)
elif node[0] == 'vlabel':
orient = wx.VERTICAL
me = wx.BoxSizer(orient)
mask = node[1]
label = node[2]
child = node[3]
me.Add(wx.StaticText(rootWidget, -1, label), 0, wx.LEFT|wx.RIGHT, 5)
me.Add(child, 0, wx.EXPAND|wx.ALL, 5)
elif node[0] == '~':
if parent: parent.AddStretchSpacer()
else:
me = node[0]
if len(node) > 1: mask = node[1]
prop = 0
style = 0
if parentOrient == wx.HORIZONTAL:
if 'h' in mask:
prop = 1
if 'v' in mask:
style |= wx.EXPAND
elif parentOrient == wx.VERTICAL:
if 'v' in mask:
prop = 1
if 'h' in mask:
style |= wx.EXPAND
if 'a' in mask: style |= wx.ALL
if 't' in mask: style |= wx.TOP
if 'b' in mask: style |= wx.BOTTOM
if 'l' in mask: style |= wx.LEFT
if 'r' in mask: style |= wx.RIGHT
if 'c' in mask: style |= wx.ALIGN_CENTER
if (parent is not None) and (me is not None):
parent.Add(me, prop, style, gap)
if me is not None:
for child in children:
BoxLayout(rootWidget, child, me, orient, gap)
return me
if __name__ == '__main__':
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, style=wx.DEFAULT_FRAME_STYLE)
self.SetBackgroundColour(wx.NullColour)
self.buttons = [ wx.Button(self, -1, str(i)) for i in range(4) ]
self.textCtrls = [ wx.TextCtrl(self) for i in range(4) ]
self.SetSizer(BoxLayout(self,
['vbox', 'h', [
['hgroup', 'ha', 'Middle Spacer',
[self.buttons[0], '~', self.buttons[1], self.buttons[2]
]],
['vgroup', 'ha', 'Left Spacer', [
['hlabel', 'h', 'Label-0', self.textCtrls[0]],
['hlabel', 'h', 'Label-1', self.textCtrls[1]],
['vlabel', 'h', 'Label-2', self.textCtrls[2]],
['vlabel', 'h', 'Label-3', self.textCtrls[3]],
['hbox', 'h', ['~', self.buttons[3]]]
]],
]]))
self.Fit()
class App(wx.App):
def OnInit(self):
frame = MainFrame()
frame.Show()
return True
app = App()
app.MainLoop()