python - 必须调用 wx.Panel 两次才能设置正确的大小

标签 python wxpython

import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title='Test',size=wx.Size(500,500))
        self.panel = wx.Panel(self, -1,pos=(20,40),size=wx.Size(10,500))
        self.panel = wx.Panel(self, -1,size=wx.Size(500,10))
        self.Show()

if __name__ == '__main__':
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

此代码将在同一窗口内创建两个面板对象。 如果删除 wx.Panel 调用的任何一行,则另一个面板将不具有正确的大小,并且它将填满整个框架。

为什么?

最佳答案

请参阅 wx.Frame 的文档:

wx.Frame processes the following events:
- wxEVT_SIZE: if the frame has exactly one child window, not counting the status and toolbar, this child is resized to take the entire frame client area. [...]

您可以定义自己的Sizers指定不同的行为:

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title='Test',size=wx.Size(500,500))

        self.panel = wx.Panel(self, -1,size=wx.Size(500,10))
        self.sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.sizer.Add(self.panel)
        self.SetSizer(self.sizer)

        self.Show()

关于python - 必须调用 wx.Panel 两次才能设置正确的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58404737/

相关文章:

python - Mac OSX 上 WxPython TaskBarIcon 的鼠标事件未触发

python - 在 wxPython 中刷新面板内容

python - wxpython 从 ID 访问对象

python - 类型错误 : only integer scalar arrays can be converted to scalar index while converting dataframe "to_datetime"

python - 使用 scikit-bio write 写入多个 fasta 条目

python - 在 Django 中编写一个非常基本的搜索表单

python - wxPython的基础

python - 用 Cython 包装包含 wxString 的 C++ 类

python - aws 使用 python Flask 不断返回 502 bad gateway 错误

python - 多层次的 islice 和循环