0

请注意:这不是wxPython 的副本:在同一帧中显示多个小部件,这是对问题的额外疑问。

好的,我已经在同一框架上有一个笔记本和一个按钮。源代码:

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 尺寸器。我该怎么做呢?

4

1 回答 1

2

考虑尺寸器和面板的最佳方式是它们是容器。对于复杂的布局,我建议拿一支铅笔和一张纸勾勒出你的布局。然后在小部件周围绘制框。这些盒子是你的尺寸器(或者有时它们是面板和尺寸器)。不管怎样,你会很快从所有的图纸中意识到盒子可以相互嵌套。这就是 BoxSizer 的关键所在。你可以嵌套它们!它可能会让人感到困惑,但如果你的代码真的很清晰并且设计得很好,它就会很好用。

无论如何,我的散文已经足够了,这是代码:

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()
于 2013-06-27T13:23:15.407 回答