sanders
sanders

Reputation: 1101

wxPython SetMinSize seems to set size explicitly, not allows window to grow when needed

Considering the code below. I create a StaticText on which I set a Minimum size. However, the StaticText always gets this size as the actual size. Even though the text does not fit. Am I doing something wrong here ?

I want to use this functionality to create a key-value display with proper alignment of the values and only expanding the key part if needed:

a key       - the value
another key - another value
a big key which does not fit - this value
the key     - the value
import wx


class TestPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        sizer = wx.BoxSizer()

        static_text = wx.StaticText(
            parent=self, label="a long label exceeding min size."
        )
        static_text.SetMinSize(wx.Size(50, -1))

        sizer.Add(static_text)
        self.SetSizer(sizer)


if __name__ == "__main__":
    app = wx.App()
    frm = wx.Frame(None, title="Test MinSize")
    pnl = TestPanel(frm)
    frm.Show()
    app.MainLoop()

Upvotes: 0

Views: 495

Answers (1)

Rolf of Saxony
Rolf of Saxony

Reputation: 22433

This may not be want to you want to hear but here goes.

The sizer performs a dance between the programmers desire to display data and the variable nature of that data. Additionally it has to cope with that pesky external thing we refer to as the user, who has a habit of resizing windows.

SetMinSize is an instruction to the sizer, a hint if you like, on what it should attempt to do automatically. Most controls will also set the minimal size to the size given in the control’s constructor, as a best guess.

These instructions can be overridden or adjusted with the proportion and flag values of the sizer entry for that control. Always keep in mind that other controls within the same sizer will be affected, which can have undesirable results on presentation.

If we give the sizer the wx.EXPAND flag for that control, it will show the whole widget whilst keeping the MinSize. In this case, expanding it vertically.

If we give the sizer a proportion of 1 it will stretch it as much as it can in the direction of the sizer, relative to other controls sharing that sizer.

To appreciate what goes on, it's best to play with code like this, altering the MinSize, proportion and flags, testing each change, until the voodoo of sizers becomes slightly less obscure.

Note: In addition, also test re-sizing the window, to see what happens in each scenario.

Additional testing notes:

  • Test sizers with more than 1 control or widget
  • Assign colours to highlight what isn't always obvious.
import wx

class TestPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        sizer = wx.BoxSizer(wx.HORIZONTAL)

        static_text = wx.StaticText(
            parent=self, label="a long label exceeding min size.")
        static_text2 = wx.StaticText(self, label="another label")
        static_text.SetBackgroundColour('green')
        static_text.SetMinSize(wx.Size(50, -1))

        sizer.Add(static_text, proportion=1, flag=wx.ALL, border=0)
        sizer.Add(static_text2, 0, wx.EXPAND, 0)
        self.SetSizer(sizer)


if __name__ == "__main__":
    app = wx.App()
    frm = wx.Frame(None, title="Test MinSize", size=(300,100))
    pnl = TestPanel(frm)
    frm.Show()
    app.MainLoop()

Examples for the above code:
enter image description here enter image description here

enter image description here enter image description here

Upvotes: 2

Related Questions