简体   繁体   中英

wxPython: Second Row in FlexGridSizer not visible on Ubuntu 16.04

Yet another wxPython GUI odyssey and again I seem to be stuck with the problem exposed by the minimal working examplyou can find below:

On macOS Sierra 10.12.6 (wxPython version: 4.0.0b2) I can see the second row ("Text2_fixed Text2_grow") in the FlexGridSizer as soon as I use sizer.SetSizeHints(static_box1): display on macOS

On Ubuntu 16.04 (wxPython version: 4.0.0a2) I cannot see the second row ("Text2_fixed Text2_grow"): display on Ubuntu

Does anyone have an idea how to make the second row visible on both environments?

Here's the minimal working example:

    import wx.lib.inspection

    app = wx.App()
    frame = wx.Frame(None, title="Top Level Frame", name="Top Level Frame", size=(400, 200))

    box_sizer = wx.BoxSizer(wx.VERTICAL)

    static_box1 = wx.StaticBox(frame, label='Static Box 1', name='Static Box 1', style=0)
    static_box1.SetBackgroundColour(wx.Colour(255, 0, 0, 255))
    static_box2 = wx.StaticBox(frame, label='Static Box 2', name='Static Box 2', style=0)
    static_box2.SetBackgroundColour(wx.Colour(0, 255, 0, 255))

    box_sizer.Add(static_box1, flag=wx.EXPAND)
    box_sizer.Add(static_box2, flag=wx.EXPAND)

    flex_grid_sizer = wx.FlexGridSizer(2, 2, 0, 0)
    flex_grid_sizer.AddGrowableCol(1)

    flex_grid_sizer.Add(wx.StaticText(static_box1, label='Text1_fixed', name='Text1_fixed', style=0),
                        flag=wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=0)
    flex_grid_sizer.Add(wx.StaticText(static_box1, label='Text1_grow', name='Text1_grow', style=0),
                        flag=wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=0)
    flex_grid_sizer.Add(wx.StaticText(static_box1, label='Text2_fixed', name='Text2_fixed', style=0),
                        flag=wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=0)
    flex_grid_sizer.Add(wx.StaticText(static_box1, label='Text2_grow', name='Text2_grow', style=0),
                        flag=wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=0)

    #SetSizeHints fixes the problem on macOS, but not on Ubuntu
    flex_grid_sizer.SetSizeHints(static_box1)
    static_box1.SetSizer(flex_grid_sizer)
    frame.SetSizer(box_sizer)
    frame.Center()
    frame.Show()
    wx.lib.inspection.InspectionTool().Show()
    app.MainLoop()

Thanks for your help!

Since you are creating the frame with aa fixed size then there may not be another EVT_SIZE event after the widgets and sizers have been created. Since the default EVT_SIZE handler is where the sizer is triggered to do its layout, then that means there will not be an initial layout of the widgets. They will just be left at their default sizes and locations.

An easy workaround will take care of this problem for you. Simply add a call to frame.SendSizeEvent after everything has been created and set, to get an extra event so the sizers will be able to do their layout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM