optagon
optagon

Reputation: 11

Why does my horizontal sizers change width after adding them to a vertical sizer?

When I only hade one TxtCtrl, it's right side would expand all the way to the windows edge. But after I created a vertical BoxSizer to and added the horizontal one to that, the TxtCtrol only had a width of about 100 pixels. Why is this?

import wx

class MainPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, style=wx.SIMPLE_BORDER)

        top_box = wx.BoxSizer(wx.VERTICAL)

        box1 = wx.BoxSizer(wx.HORIZONTAL)
        box2 = wx.BoxSizer(wx.HORIZONTAL)
        textureName = wx.TextCtrl(self, 1)
        texturePath = wx.TextCtrl(self, 1)

        box1.Add(wx.StaticText(self, 1, "Name: "), 0, wx.LEFT|wx.RIGHT|wx.TOP, 5)
        box1.Add(textureName, 1, wx.ALIGN_LEFT|wx.RIGHT|wx.TOP, 5)

        box2.Add(wx.StaticText(self, 1, "Path:     "), 0, wx.LEFT|wx.RIGHT|wx.TOP, 5)
        box2.Add(texturePath, 1, wx.ALIGN_LEFT|wx.RIGHT|wx.TOP, 5)

        top_box.Add(box1)
        top_box.Add(box2)

        self.SetSizer(top_box)

class MainFrame(wx.Frame):
    def __init__(self, parent, id):
        title = "Exporter"
        wx.Frame.__init__(self, parent, wx.ID_ANY, title,
            size=(500, 420),
            style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
        self.panel = MainPanel(self)

        self.Show()

if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MainFrame(None, -1)
    frame.Centre()
    app.MainLoop()
    pass

Upvotes: 1

Views: 57

Answers (1)

tom10
tom10

Reputation: 69232

You need to use wx.EXPAND in the style.

top_box.Add(box1, 0, wx.EXPAND)
top_box.Add(box2)

enter image description here

From here: "When an item is Add'ed with the wxEXPAND flag, the item will be resized to fill its alloted area in the opposite orientation."

Upvotes: 1

Related Questions