在 wxPython 中,Sizers 用于布置窗口或窗口集合。
笔记本也不例外,因为它也可以使用 sizer 或 sizer 集合来布局其标签。
Sizer 允许定位和对齐。
import wx
class ResultFrame(wx.Frame):
def __init__(self, parent):
super(ResultFrame, self).__init__(parent, -1, title = "Results",
style=wx.CAPTION|wx.CLOSE_BOX|wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.RESIZE_BORDER)
#self.targets = ['these','are','some','strings']
self.SetMinSize(wx.Size(1000,-1))
self.InitUI()
def InitUI(self):
self.mainpnl = wx.Panel(self)
self.mainpnl.SetSize(wx.Size(1590,900))
self.mainsizer = wx.BoxSizer(wx.HORIZONTAL)
self.ctrlsizer = wx.BoxSizer(wx.VERTICAL)
self.restart = wx.Button(self.mainpnl, wx.ID_ANY, "Kubla Khan",\
size=wx.Size(150,90))
self.interp = wx.Button(self.mainpnl, wx.ID_ANY, "Samuel Taylor Coleridge\n"
"Biography", size=wx.Size(150,90))
self.nb = wx.Notebook(self.mainpnl)
# for idx, tar in enumerate(self.targets):
# self.nb.AddPage(wx.Panel(self.nb, -1, size=wx.Size(850,820)), tar)
tab1 = wx.Panel(self.nb, -1)
tab2 = wx.Panel(self.nb, -1)
msg = '''In Xanadu did Kubla Khan
A stately pleasure dome decree:
Where Alph, the sacred river, ran
Through caverns measureless to man
Down to a sunless sea.
So twice five miles of fertile ground
With walls and towers were girdled round:
And there were gardens bright with sinuous rills,
Where blossomed many an incense-bearing tree;
And here were forests ancient as the hills,
Enfolding sunny spots of greenery.'''
t1_text = wx.StaticText(tab1, -1, label=msg)
t1_butt = wx.Button(tab1, -1, label="Next")
t1sizer = wx.BoxSizer(wx.VERTICAL)
t1sizer.Add(t1_text, 0, wx.ALIGN_CENTER|wx.ALL, 5)
t1sizer.Add(t1_butt, 0, wx.ALIGN_RIGHT|wx.ALL, 5)
tab1.SetSizer(t1sizer)
self.nb.AddPage(tab1,"Stanza 1")
msg = '''But oh! that deep romantic chasm which slanted
Down the green hill athwart a cedarn cover!
A savage place! as holy and enchanted
As e'er beneath a waning moon was haunted
By woman wailing for her demon lover!
And from this chasm, with ceaseless turmoil seething,
As if this earth in fast thick pants were breathing,
A mighty fountain momently was forced:
Amid whose swift half-intermitted burst
Huge fragments vaulted like rebounding hail,
Or chaffy grain beneath the thresher's flail:
And ’mid these dancing rocks at once and ever
It flung up momently the sacred river.
Five miles meandering with a mazy motion
Through wood and dale the sacred river ran,
Then reached the caverns measureless to man,
And sank in tumult to a lifeless ocean:
And ’mid this tumult Kubla heard from far
Ancestral voices prophesying war!'''
t2_text = wx.StaticText(tab2, -1, label=msg)
t2_pbutt = wx.Button(tab2, -1, label="Prev")
t2_nbutt = wx.Button(tab2, -1, label="Next")
t2sizer = wx.BoxSizer(wx.VERTICAL)
t2sizer.Add(t2_text, 0, wx.ALIGN_CENTER|wx.ALL, 5)
t2sizer.Add(t2_pbutt, 0, wx.ALIGN_RIGHT|wx.ALL, 5)
t2sizer.Add(t2_nbutt, 0, wx.ALIGN_RIGHT|wx.ALL, 5)
tab2.SetSizer(t2sizer)
self.nb.AddPage(tab2,"Stanza 2")
self.ctrlsizer.Add(self.restart, 0, wx.ALL, 5)
self.ctrlsizer.AddStretchSpacer()
self.ctrlsizer.Add(self.interp, 0, wx.ALL, 5)
self.mainsizer.Add(self.ctrlsizer, 0, wx.ALL, 0)
self.mainsizer.Add(self.nb, 1, wx.EXPAND)
self.mainpnl.SetSizerAndFit(self.mainsizer)
t1_butt.Bind(wx.EVT_BUTTON, self.Next)
t2_nbutt.Bind(wx.EVT_BUTTON, self.Next)
t2_pbutt.Bind(wx.EVT_BUTTON, self.Prev)
def Next(self, event):
p = self.nb.GetSelection()
p += 1
p = min(p, self.nb.GetRowCount())
self.nb.SetSelection(p)
def Prev(self, event):
p = self.nb.GetSelection()
p -= 1
p = max(0, p)
self.nb.SetSelection(p)
def main():
app = wx.App()
frm=ResultFrame(None)
frm.Show()
app.MainLoop()
if __name__ == "__main__":
main()