Window sizing using "Fit" only works for default font size Topic is solved

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
Post Reply
MrSurly
In need of some credit
In need of some credit
Posts: 6
Joined: Thu Nov 26, 2020 6:52 pm

Window sizing using "Fit" only works for default font size

Post by MrSurly »

The code below is working for me, with one small hiccup. Using col1sizer.Fit() in __init__ works as expected. Without this, the window is the wrong size. With this call, the window exactly fits the contents.

But then I wanted to change the font size, so I added the self.SetFont() call, which also works as expected; all child items are now using this font.

BUT, the window size from the col1sizer.Fit() resizes the size of the content with the default font. That is to say the window size does not change to fit the new font size -- it's exactly the size it was when the SetFont() call wasn't there. So if I use a smaller font size, the window is too big, and if I use a larger font size, the window is too small.

I added the InvalidateBestSize() in an effort to fix this, but it had no effect.

Without SetFont():
size_ok.png
size_ok.png (45.96 KiB) Viewed 10023 times
With SetFont():
size_bad.png
size_bad.png (28.18 KiB) Viewed 10023 times
Full code below:

Code: Select all

#!/usr/bin/env python
import wx

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        self._defaultBorder = wx.EXPAND | wx.TOP | wx.BOTTOM | wx.LEFT | wx.RIGHT, 7

        font = wx.Font(5, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL)
        self.SetFont(font)
        col1Sizer = wx.BoxSizer(wx.VERTICAL)


        self.basicConfig(col1Sizer)
        #self.highProtectConfig()

        #self.SetAutoLayout(True)
        self.SetSizer(col1Sizer)
        self.InvalidateBestSize()
        col1Sizer.Fit(self)

    def basicConfig(self, sizer):
        panel = wx.Panel(self)

        sb = wx.StaticBox(panel, label='Basic Configuration')
        sbs = wx.StaticBoxSizer(sb, wx.VERTICAL)
        fgs = wx.FlexGridSizer(11, gap=(3,3))
        sbs.Add(fgs, 0, wx.EXPAND | wx.ALL, border=5)
        panel.SetSizer(sbs)
        sizer.Add(panel, 1, *self._defaultBorder)

        rflags = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT
        lflags = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_LEFT

        def gen(fn, unit1, unit2 = None, unit3 = 'S', spacing=10):
            unit2 = unit2 or unit1
            items = (
                (wx.StaticText(sb, label = fn.upper()), 0, rflags),
                (wx.TextCtrl(sb, name = fn), 0, lflags),
                (wx.StaticText(sb, label = unit1), 0, rflags),
                (spacing, 1),
                (wx.StaticText(sb, label = 'Release'), 0, rflags),
                (wx.TextCtrl(sb, name = fn + '_rel'), 0, lflags),
                (wx.StaticText(sb, label = unit2), 0, lflags),
                (spacing, 1),
                (wx.StaticText(sb, label = 'Delay'), 0, rflags),
                (wx.TextCtrl(sb, name = fn + '_delay'), 0, lflags),
                (wx.StaticText(sb, label = unit3), 0, lflags),
            )
            return items

        fgs.AddMany(gen('covp', 'mV'))
        fgs.AddMany(gen('cuvp', 'mV'))
        fgs.AddMany(gen('povp', 'mV'))
        fgs.AddMany(gen('puvp', 'mV'))
        fgs.AddMany(gen('chgot', 'C'))
        fgs.AddMany(gen('chgut', 'C'))
        fgs.AddMany(gen('dsgot', 'C'))
        fgs.AddMany(gen('dsgut', 'C'))
        fgs.AddMany(gen('chgoc', 'mA', 'S'))
        fgs.AddMany(gen('dsgoc', 'mA', 'S'))

        sbs.Fit(panel)

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, 'A Static box', )
        frame.Show()
        return True

if __name__ == "__main__":
    app = MyApp()
    app.MainLoop()
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Window sizing using "Fit" only works for default font size

Post by doublemax »

Which wxWidgets version is your wxPython version based on?

The issue seems to be related to this: https://trac.wxwidgets.org/ticket/16088#comment:4
The workaround before the fix was to catch the wxShowEvent of the main frame and re-calculate the layout then
Use the source, Luke!
MrSurly
In need of some credit
In need of some credit
Posts: 6
Joined: Thu Nov 26, 2020 6:52 pm

Re: Window sizing using "Fit" only works for default font size

Post by MrSurly »

Python 3.8.3 (default, May 19 2020, 18:47:26)
wxPython 4.1.0

I'm on Ubuntu 20.04, using Anaconda.
MrSurly
In need of some credit
In need of some credit
Posts: 6
Joined: Thu Nov 26, 2020 6:52 pm

Re: Window sizing using "Fit" only works for default font size

Post by MrSurly »

@doublemax:

Thank you, this worked.
Post Reply