Search code examples
pythonlayoutwxpythonboxsizer

How to fix the layout stacking in wxpython


I'm trying to display the connection settings, that are required for an ssh connection, i'm using the wx.BoxSizer to arrange the layout, unfortunately the layout doesn't work and stacks all elements in the top left corner (Until i resize the window by scaling/maximizing).

I've already tried to use:self.Update(),self.Refresh() and self.Layout() after i called self.Show(True) method but this doesn't work for me. If i remove the section 'statusbar setup' and 'creating the menubar' it works but i need those.

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

this is what it's initially showing: https://i.sstatic.net/zESSn.jpg and this is how it's supposed to look like: https://i.sstatic.net/84IoK.jpg The second result is showing as soon as i rescale the window.


Solution

  • You are really close. You should only add the line:

    vboxMain.Fit(panel)
    

    below the line:

    panel.SetSizer(vboxMain)