wxpython:嵌套的大小调整器和左上角的小方块?

发布于 2024-10-20 03:49:27 字数 1665 浏览 0 评论 0原文

我正在尝试研究 wxPython 和 sizers,并将以下示例放在一起:

import wx

class MyTestFrame(wx.Frame):
    def __init__(self, parent, title):
        super(MyTestFrame, self).__init__(parent, title=title, 
            size=(250, 150))

        # the master panel of the frame - "Add a panel so it looks correct on all platforms"
        self.panel = wx.Panel(self, wx.ID_ANY)
            # self.panel.SetBackgroundColour(wx.Colour(124, 224, 124)) # to confirm the square is the panel


        # want these buttons absolutely positioned
        btn_A = wx.Button(self, id=1, label='A', pos=(10, 10), size=(30, 30))
        btn_A.SetBackgroundColour(wx.Colour(224, 124, 124))
        btn_B = wx.Button(self, id=2, label='B', pos=(45, 10), size=(30, 30))
        btn_C = wx.Button(self, id=3, label='C', pos=(80, 10), size=(30, 30))

        # additional object

        mastersizer = wx.BoxSizer(wx.VERTICAL)
        btnsizer = wx.BoxSizer(wx.HORIZONTAL)

        btnsizer.Add(btn_A, 0)
        btnsizer.Add(btn_B, 0)
        btnsizer.Add(btn_C, 0)

        mastersizer.Add(btnsizer, 1, wx.EXPAND)
        self.panel.SetSizer(mastersizer)
        #~ mastersizer.Fit(self) # makes the window as large as the buttons

        self.Centre()
        self.Show()


if __name__ == '__main__':
    app = wx.App()
    MyTestFrame(None, 'Test')
    app.MainLoop()

当我运行此示例时,我会得到一个如图所示的窗口:

wxPython window

有人可以解释一下,为什么我会在左上角看到那个灰色的小方块 - 实现代码的正确方法是什么? (该按钮故意着色,因此可以很明显..)我使用的是 Ubuntu Lucid,以防这是特定于平台的。

编辑:那个方块显然是面板本身,但我不知道为什么它不按预期调整大小并成为按钮的“父级”?

I'm trying to look into wxPython and sizers, and I put together the following example:

import wx

class MyTestFrame(wx.Frame):
    def __init__(self, parent, title):
        super(MyTestFrame, self).__init__(parent, title=title, 
            size=(250, 150))

        # the master panel of the frame - "Add a panel so it looks correct on all platforms"
        self.panel = wx.Panel(self, wx.ID_ANY)
            # self.panel.SetBackgroundColour(wx.Colour(124, 224, 124)) # to confirm the square is the panel


        # want these buttons absolutely positioned
        btn_A = wx.Button(self, id=1, label='A', pos=(10, 10), size=(30, 30))
        btn_A.SetBackgroundColour(wx.Colour(224, 124, 124))
        btn_B = wx.Button(self, id=2, label='B', pos=(45, 10), size=(30, 30))
        btn_C = wx.Button(self, id=3, label='C', pos=(80, 10), size=(30, 30))

        # additional object

        mastersizer = wx.BoxSizer(wx.VERTICAL)
        btnsizer = wx.BoxSizer(wx.HORIZONTAL)

        btnsizer.Add(btn_A, 0)
        btnsizer.Add(btn_B, 0)
        btnsizer.Add(btn_C, 0)

        mastersizer.Add(btnsizer, 1, wx.EXPAND)
        self.panel.SetSizer(mastersizer)
        #~ mastersizer.Fit(self) # makes the window as large as the buttons

        self.Centre()
        self.Show()


if __name__ == '__main__':
    app = wx.App()
    MyTestFrame(None, 'Test')
    app.MainLoop()

When I run this, I get a window like on the image:

wxPython window

Can someone explain, why do I get that gray little square in the upper left corner - and what would be the correct way to implement the code? (The button is deliberately colored, so it can be obvious.. ) I'm on Ubuntu Lucid, in case this is platform specific.

EDIT: That square is apparently the panel itself, but then I cannot tell why doesn't it resize and become the 'parent' for the buttons, as intended?

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

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

发布评论

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

评论(1

微暖i 2024-10-27 03:49:27

啊,好吧——还不错;但对我来说并不是那么明显:) 所以这里有一些像我这样的菜鸟的参考:在上面的代码中,按钮被定义为框架的子级 - 而不是面板的子级;所以唯一的变化是:

    # want these buttons absolutely positioned
    # must be children of panel - if panel is to encompass them! 
    btn_A = wx.Button(self.panel, id=1, label='A', pos=(10, 10), size=(30, 30))
    btn_A.SetBackgroundColour(wx.Colour(224, 124, 124))
    btn_B = wx.Button(self.panel, id=2, label='B', pos=(45, 10), size=(30, 30))
    btn_C = wx.Button(self.panel, id=3, label='C', pos=(80, 10), size=(30, 30))

然后一切看起来都很好:

在此处输入图像描述

好吧,很抱歉在这里浪费了空间 -但希望对其他人有用:)

干杯!

Ah well - wasn't that bad; but wasn't that obvious to me :) So here's some reference for other noobs like myself: In the above code, the buttons are defined as children of the frame - not of the panel; so the only change is this:

    # want these buttons absolutely positioned
    # must be children of panel - if panel is to encompass them! 
    btn_A = wx.Button(self.panel, id=1, label='A', pos=(10, 10), size=(30, 30))
    btn_A.SetBackgroundColour(wx.Colour(224, 124, 124))
    btn_B = wx.Button(self.panel, id=2, label='B', pos=(45, 10), size=(30, 30))
    btn_C = wx.Button(self.panel, id=3, label='C', pos=(80, 10), size=(30, 30))

And then all seems fine:

enter image description here

Well, sorry to have wasted space here - but hopefully, may be of use to others :)

Cheers!

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