wxpython:如何设置wxnotebook的宽度和高度?

0 投票
1 回答
2289 浏览
提问于 2025-04-17 05:22

我遇到了一些麻烦,我觉得可能是和大小设置有关。我无法按照我想要的方式来设置笔记本的大小。现在我放了一个按钮,按钮的位置决定了笔记本的大小(它会把笔记本撑大),但是我希望能够不使用按钮就能做到这一点。我试着调整了一些设置,但就是无法让它自己改变大小。

如果你不明白我说的是什么意思,请运行以下代码。

import random
import wx


[wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1BUTTON2, wxID_FRAME1LISTBOX1, 
] = [wx.NewId() for _init_ctrls in range(4)]
########################################################################
class TabPanel(wx.Panel):
    #----------------------------------------------------------------------
    def __init__(self, parent):
        """"""
        wx.Panel.__init__(self, parent=parent)

        colors = ["red", "blue", "gray", "yellow", "green"]
        self.SetBackgroundColour(random.choice(colors))

        sampleList = ['0', '1', '2', '3', '4']
        listBox = wx.ListBox(self, -1, (20, 20), (80, 120), sampleList, wx.LB_SINGLE)
        listBox.SetSelection(3)
        #This is the button!!!!
        btn = wx.Button(self, label="Create new")
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(btn,300,400, wx.ALL, 10)
        self.SetSizer(sizer)

########################################################################
class DemoFrame(wx.Frame):
    """
    Frame that holds all other widgets
    """

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, wx.ID_ANY,
                          "Notebook Tutorial",
                          size=(600,400)
                          )
        panel = wx.Panel(self)

        notebook = wx.Notebook(panel)
        tabOne = TabPanel(notebook)
        notebook.AddPage(tabOne, "things")

        tabTwo = TabPanel(notebook)
        notebook.AddPage(tabTwo, "other things")

        sizer = wx.BoxSizer(400)
        sizer.Add(notebook)
        panel.SetSizer(sizer)
                        #menu bar
        status=self.CreateStatusBar()
        menubar=wx.MenuBar()
        first=wx.Menu()
        second=wx.Menu()
        first.Append(wx.NewId(),"New","Creates A new file")
        first.Append(wx.NewId(),"ADID","Yo")
        menubar.Append(first,"File")
        menubar.Append(second,"Edit")
        self.SetMenuBar(menubar)
        self.Layout()

        self.Show()

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = DemoFrame()
    app.MainLoop()

非常感谢你的关注。

1 个回答

0

试试这样做:

COLORS = ["red", "blue", "black", "yellow", "green"]
NUMBERS = ['0', '1', '2', '3', '4']
PANELS = ["Things", "More things", "Ultra!"]

import random
import wx

class TabPanel(wx.Panel):
    def __init__(self, *args, **kwargs):
        wx.Panel.__init__(self, *args, **kwargs)
        self.SetBackgroundColour(random.choice(COLORS))
        self.listBox = wx.ListBox(self, size=(200, -1), choices=NUMBERS, style=wx.LB_SINGLE)
#        self.button = wx.Button(self, label="Something else here? Maybe!")

        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.listBox, proportion=0, flag=wx.ALL | wx.EXPAND, border=5)
#        self.sizer.Add(self.button, proportion=1, flag=wx.ALL)

        self.SetSizer(self.sizer)


class MyNotebook(wx.Notebook):
    def __init__(self, *args, **kwargs):
        wx.Notebook.__init__(self, *args, **kwargs)

        self.panels = []
        for name in PANELS:
            panel = TabPanel(self)
            self.panels.append(panel)
            self.AddPage(panel, name)


class MyPanel(wx.Panel):
    def __init__(self, *args, **kwargs):
        wx.Panel.__init__(self, *args, **kwargs)

        self.notebook = MyNotebook(self, size=(400, -1))
#        self.button = wx.Button(self, label="Something else here? Maybe!")

        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.notebook, proportion=0, flag=wx.EXPAND)
#        self.sizer.Add(self.button, proportion=0)
        self.SetSizer(self.sizer)


class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        self.panel = MyPanel(self)

        self.status = self.CreateStatusBar()

        self.menubar = wx.MenuBar()
        first=wx.Menu()
        second=wx.Menu()
        first.Append(wx.NewId(), "New", "Creates A new file")
        first.Append(wx.NewId(), "ADID", "Yo")
        self.menubar.Append(first, "File")
        self.menubar.Append(second, "Edit")
        self.SetMenuBar(self.menubar)

        self.Show()


app = wx.App(False)
win = MainWindow(None, size=(600, 400))
app.MainLoop()

把按钮的注释去掉,看看你怎么能添加更多的东西。你可能会在我的代码中找到一些对你学习wxPython编程有帮助的提示。我懒得一一解释 ;-).

撰写回答