pythonwxpythonpython-3.8wxglade

Why does this StaticLine appear behind the sizer?


I'm trying to draw a static line between the "Label"+TextCtrl and the radio buttons. It keeps appearing only when the window is dragged, and then it appears behind everything and I can't figure out why.

The goal is to have the static line draw horizontally between the upper and the lower section.

import wx

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame.__init__
        wx.Frame.__init__(self, *args, **kwds)
        self.SetSize((237, 237))

    self.__initUI()
    self.__do_layout()

def __initUI(self):

    panel = wx.Panel(self)
    self.SetSize((350, 150))
    self.qtyField = wx.TextCtrl(panel, wx.ID_ANY, "", style=wx.TE_CENTER)
    self.qtyField.SetFocus()
    self.longRb = wx.RadioButton(panel, wx.ID_ANY, "This", style=wx.RB_GROUP)
    self.shortRb = wx.RadioButton(panel, wx.ID_ANY, "That")

def __do_layout(self):
    # begin wxGlade: MyFrame.__do_layout
    vertSizer = wx.BoxSizer(wx.VERTICAL)
    horSizer1 = wx.GridSizer(1, 2, 0, 0)
    rbSizer = wx.GridSizer(1, 2, 0, 36)

    qtyLabel = wx.StaticText(self, wx.ID_ANY, "Label")
    horSizer1.Add(qtyLabel, 0, wx.ALIGN_CENTER, 0)
    horSizer1.Add(self.qtyField, 0, wx.ALIGN_CENTER_VERTICAL, 0)
    vertSizer.Add(horSizer1, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM | wx.TOP, 6)

    static_line_1 = wx.StaticLine(self, wx.ID_ANY)
    vertSizer.Add(static_line_1, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 6)

    rbSizer.Add(self.longRb, 0, wx.ALIGN_CENTER, 0)
    rbSizer.Add(self.shortRb, 0, wx.ALIGN_CENTER, 0)
    vertSizer.Add(rbSizer, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM | wx.TOP, 6)

    self.SetSizer(vertSizer)


class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, wx.ID_ANY, "")
        self.SetTopWindow(self.frame)
        self.frame.Show()
        return True

# end of class MyApp

if __name__ == "__main__":
    app = MyApp(0)
    app.MainLoop()

Solution

  • Your are assigning some widgets to the Frame (self) and others to the panel, so they present themselves where instructed to do so.

    This is what you are after:

    import wx
    
    class MyFrame(wx.Frame):
        def __init__(self, *args, **kwds):
            # begin wxGlade: MyFrame.__init__
            wx.Frame.__init__(self, *args, **kwds)
            self.SetSize((237, 237))
    
            self.__initUI()
            self.__do_layout()
    
        def __initUI(self):
    
            self.panel = wx.Panel(self)
            self.panel.SetBackgroundColour("green")
            self.SetSize((350, 150))
            self.qtyField = wx.TextCtrl(self.panel, wx.ID_ANY, "", style=wx.TE_CENTER)
            self.qtyField.SetFocus()
            self.longRb = wx.RadioButton(self.panel, wx.ID_ANY, "This", style=wx.RB_GROUP)
            self.shortRb = wx.RadioButton(self.panel, wx.ID_ANY, "That")
    
        def __do_layout(self):
            # begin wxGlade: MyFrame.__do_layout
            vertSizer = wx.BoxSizer(wx.VERTICAL)
            horSizer1 = wx.GridSizer(1, 2, 0, 0)
            rbSizer = wx.GridSizer(1, 2, 0, 36)
    
            qtyLabel = wx.StaticText(self.panel, wx.ID_ANY, "Label")
            horSizer1.Add(qtyLabel, 0, wx.ALIGN_CENTER, 0)
            horSizer1.Add(self.qtyField, 0, wx.ALIGN_CENTER_VERTICAL, 0)
            vertSizer.Add(horSizer1, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM | wx.TOP, 6)
    
            static_line_1 = wx.StaticLine(self.panel, wx.ID_ANY)
            vertSizer.Add(static_line_1, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 6)
    
            rbSizer.Add(self.longRb, 0, wx.ALIGN_CENTER, 0)
            rbSizer.Add(self.shortRb, 0, wx.ALIGN_CENTER, 0)
            vertSizer.Add(rbSizer, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM | wx.TOP, 6)
    
            self.SetSizer(vertSizer)
    
    
    class MyApp(wx.App):
        def OnInit(self):
            self.frame = MyFrame(None, wx.ID_ANY, "")
            self.SetTopWindow(self.frame)
            self.frame.Show()
            return True
    
    # end of class MyApp
    
    if __name__ == "__main__":
        app = MyApp(0)
        app.MainLoop()
    

    enter image description here