我希望能够在一个帧中显示Notebook
和TxtCtrl
wx小部件.下面是一个改编自wxpython wiki的例子; 是否有可能改变他们的布局(可能有类似的东西wx.SplitterWindow
)来显示Notebook
同一帧下面的文本框?
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() 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, 'notebook.py') frame.Show(True) frame.Center() frame2 = MyFrame(None, -1, '') frame2.Show(True) self.SetTopWindow(frame2) return True app = MyApp(0) app.MainLoop()
DzinX.. 8
实际上,使两个小部件出现在同一帧上很容易.您应该使用sizer来完成此任务.
在您的示例中,您可以将Notebook
类实现更改为以下内容:
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() # new code begins here: # add your text ctrl: self.text = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE) # create a new sizer for both controls: sizer = wx.BoxSizer(wx.VERTICAL) # add notebook first, with size factor 2: sizer.Add(nb, 2) # then text, size factor 1, maximized sizer.Add(self.text, 1, wx.EXPAND) # assign the sizer to Frame: self.SetSizerAndFit(sizer)
只有__init__
方法改变了.请注意,您可以通过更改方法的第二个参数来操纵笔记本和文本控件之间的比例Add
.
您可以从官方的Sizer概述文章中了解有关sizer的更多信息.
实际上,使两个小部件出现在同一帧上很容易.您应该使用sizer来完成此任务.
在您的示例中,您可以将Notebook
类实现更改为以下内容:
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() # new code begins here: # add your text ctrl: self.text = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE) # create a new sizer for both controls: sizer = wx.BoxSizer(wx.VERTICAL) # add notebook first, with size factor 2: sizer.Add(nb, 2) # then text, size factor 1, maximized sizer.Add(self.text, 1, wx.EXPAND) # assign the sizer to Frame: self.SetSizerAndFit(sizer)
只有__init__
方法改变了.请注意,您可以通过更改方法的第二个参数来操纵笔记本和文本控件之间的比例Add
.
您可以从官方的Sizer概述文章中了解有关sizer的更多信息.