请注意:这不是wxPython: displaying multiple widgets in same frame的重复,这是对问题的疑问。
好的,我已经在同一帧上有一个Notebook和一个Button。 源代码:
import wx
import wx.lib.sheet as sheet
class MySheet(sheet.CSheet):
def __init__(self, parent):
sheet.CSheet.__init__(self, parent)
self.SetLabelBackgroundColour('#CCFF66')
self.SetNumberRows(50)
self.SetNumberCols(50)
class Notebook(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(600, 600))
menubar = wx.MenuBar()
file = wx.Menu()
file.Append(101, 'Quit', '' )
menubar.Append(file, "&File")
self.SetMenuBar(menubar)
wx.EVT_MENU(self, 101, self.OnQuit)
nb = wx.Notebook(self, -1, style=wx.NB_BOTTOM)
self.sheet1 = MySheet(nb)
self.sheet2 = MySheet(nb)
self.sheet3 = MySheet(nb)
nb.AddPage(self.sheet1, "Sheet1")
nb.AddPage(self.sheet2, "Sheet2")
nb.AddPage(self.sheet3, "Sheet3")
self.sheet1.SetFocus()
self.StatusBar()
self.button1 = wx.Button(self, -1, label = "Ok")
#self.button2 = wx.Button(self, -1, label = "Not Ok")
sizerV = wx.BoxSizer(wx.VERTICAL)
# add notebook first, with size factor 2:
sizerV.Add(nb, 5)
# then text, size factor 1, maximized
sizerV.Add(self.button1, 1, wx.EXPAND)
# assign the sizer to Frame:
self.SetSizerAndFit(sizerV)
def StatusBar(self):
self.statusbar = self.CreateStatusBar()
def OnQuit(self, event):
self.Close()
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(450, 400))
self.text = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE)
self.Center()
class MyApp(wx.App):
def OnInit(self):
frame = Notebook(None, -1, 'Pandel.py')
frame.Show(True)
frame.Center()
frame2 = MyFrame(None, -1, '')
self.SetTopWindow(frame2)
return True
app = MyApp(0)
app.MainLoop()
输出:
但我想要两个彼此相邻的按钮,而不是屏幕上的那个大按钮。
那就是我想一起使用wx.HORIZONTAL和WX.VERTICAL sizer。我该怎么做?
答案 0 :(得分:2)
考虑尺寸和面板等的最佳方法是它们是容器。对于复杂的布局,我建议抓一支铅笔和一张纸,勾勒出你的布局。然后在小部件周围绘制框。那些盒子是你的尺寸(或者有时它们是面板和尺寸)。无论如何,你会很快意识到所有的绘图都可以将盒子嵌套在一起。这是BoxSizers的关键。你可以嵌套他们!它可能会让人感到困惑,但是如果你保持你的代码非常清晰和精心设计,它就会很好用。
无论如何我的散文,这是代码:
import wx
import wx.lib.sheet as sheet
class MySheet(sheet.CSheet):
def __init__(self, parent):
sheet.CSheet.__init__(self, parent)
self.SetLabelBackgroundColour('#CCFF66')
self.SetNumberRows(50)
self.SetNumberCols(50)
class Notebook(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(600, 600))
menubar = wx.MenuBar()
file = wx.Menu()
file.Append(101, 'Quit', '' )
menubar.Append(file, "&File")
self.SetMenuBar(menubar)
wx.EVT_MENU(self, 101, self.OnQuit)
nb = wx.Notebook(self, -1, style=wx.NB_BOTTOM)
self.sheet1 = MySheet(nb)
self.sheet2 = MySheet(nb)
self.sheet3 = MySheet(nb)
nb.AddPage(self.sheet1, "Sheet1")
nb.AddPage(self.sheet2, "Sheet2")
nb.AddPage(self.sheet3, "Sheet3")
self.sheet1.SetFocus()
self.StatusBar()
self.button1 = wx.Button(self, -1, label = "Ok")
self.button2 = wx.Button(self, -1, label = "Not Ok")
sizerV = wx.BoxSizer(wx.VERTICAL)
sizerH = wx.BoxSizer(wx.HORIZONTAL)
# add notebook first, with size factor 2:
sizerV.Add(nb, 5)
# then text, size factor 1, maximized
sizerH.Add(self.button1, 1, wx.EXPAND)
sizerH.Add(self.button2, 1, wx.EXPAND)
sizerV.Add(sizerH, 1, wx.ALL|wx.EXPAND, 5)
# assign the sizer to Frame:
self.SetSizerAndFit(sizerV)
def StatusBar(self):
self.statusbar = self.CreateStatusBar()
def OnQuit(self, event):
self.Close()
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(450, 400))
self.text = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE)
self.Center()
class MyApp(wx.App):
def OnInit(self):
frame = Notebook(None, -1, 'Pandel.py')
frame.Show(True)
frame.Center()
frame2 = MyFrame(None, -1, '')
self.SetTopWindow(frame2)
return True
app = MyApp(0)
app.MainLoop()