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

发布于 2024-12-12 16:29:16 字数 2434 浏览 0 评论 0原文

我遇到了麻烦,我相信是尺寸确定者。我无法按照我想要的方式设置笔记本的大小。现在我放了一个按钮,按钮的位置决定了笔记本的大小(它将拉伸它),但是我希望它能够在不使用按钮的情况下执行此操作。我已经尝试过它,但是我无法让它自行改变。

如果您不明白我的意思,请运行以下代码。

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()

非常感谢您的浏览。

i am having troubles, with I believe is the sizers. I cannot set the size of the notebook the way i want it to be. Right now I put a button and the position of the button determines the size of the notebook (it will stretch it) however i want it to just be able to do this without using the button. I have played around with it however i cannot get it to change by itself.

Please run the following code if you do not understand what I mean.

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()

Thank you very so much for looking.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落成白 2024-12-19 16:29:16

尝试这样的操作:

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 编程有用的提示。我懒得描述一切;-)。

Try something like this:

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()

Uncomment the buttons to see how you can add more stuff. You may find some useful hints for your wxPython programming in my code. I am to lazy to describe everything ;-).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文