如何修复wxpython中的布局堆叠

How to fix the layout stacking in wxpython

我正在尝试显示 ssh 连接所需的连接设置,我正在使用 wx.BoxSizer 来安排布局,不幸的是布局不起作用并将所有元素堆叠在左上角(直到我将 window 的大小调整为 scaling/maximizing)。

我已经尝试 use:self.Update(),self.Refresh() 和 self.Layout() 在我调用 self.Show(True) 方法之后,但这对我不起作用。 如果我删除 'statusbar setup' 和 'creating the menubar' 部分,它可以工作,但我需要那些。

import wx
import connectionSettingsProperties
import connectionSettings
from pubsub import pub

class MyFrame(wx.Frame):
    def __init__(self,parent,title):
        wx.Frame.__init__(self, parent, title = title, size = (1600,800))
        panel = wx.Panel(self)

        #statusbar setup
        self.CreateStatusBar()

        # menu setup
        filemenu = wx.Menu()

        # creating the menubar
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"Menu")
        self.SetMenuBar(menuBar)

        #connectionstatus init
        self.ipLabel = wx.StaticText(panel, label = 'ip:')
        self.usernameLabel = wx.StaticText(panel, label = 'username:')

        #building layout
        vboxMain = wx.BoxSizer(wx.VERTICAL)
        hboxConnection = wx.BoxSizer(wx.HORIZONTAL)
        hboxConnection.Add(self.ipLabel)
        hboxConnection.Add(self.usernameLabel)
        vboxMain.Add(hboxConnection)
        panel.SetSizer(vboxMain)

        #show content
        self.Show(True)

app = wx.App(False)
frame = MyFrame(None, 'MyApp')
app.MainLoop()

这是它最初显示的内容:https://imgur.com/VQebA9t 它应该是这样的:https://imgur.com/60V1tcF 我重新调整 window.

后立即显示第二个结果

你真的很亲近。您应该只添加行:

vboxMain.Fit(panel)

下一行:

panel.SetSizer(vboxMain)