Search code examples
pythonuser-interfacewxpythonwxwidgets

wxpython flexgridSizer button bottom right corner


I would like to put the "Enregistrer" button to the bottom right corner: enter image description here

Here is my code :

    sizer = wx.FlexGridSizer(10, 6, 10, 10)
    # here i had all the other stuff an put it in the sizer
    # self refere to  a wx.panel
    # SPACE
    for v in range(0, 40):
        sizer.Add(10,10,wx.EXPAND)

    btn = wx.Button(self, wx.ID_ANY, "Enregistrer")
    btn.Bind(wx.EVT_BUTTON, self.save)
    sizer.Add(btn, 2, wx.ALIGN_BOTTOM|wx.ALIGN_RIGHT)
    self.SetSizer(sizer)

I can't fiqure it out why the button dosen't goes to the corner.

Can you please help me ?


Solution

  • There is a mismatch between the declared size of you FlexGridSizer (10x6) and the number of items that you are putting in it (40 + 1 button). Changing your code a tad:

    #!/usr/bin/env python
    import wx
    class MyFrame(wx.Frame):
        def __init__(self, parent, ID, title):
            wx.Frame.__init__(self, parent, ID, title, wx.DefaultPosition)
            sizer = wx.FlexGridSizer(10, 6, 10, 10)
        # here i had all the other stuff an put it in the sizer
        # self refere to  a wx.panel
        # SPACE
            v=[]
            for i in range(0,59):
                v.append(wx.StaticText(self,-1,"......"+str(i)))
            for i in v:
                sizer.Add(i,1,wx.EXPAND)
    
            btn = wx.Button(self, wx.ID_ANY, "Enregistrer")
            #btn.Bind(wx.EVT_BUTTON, self.save)
            sizer.Add(btn,0,wx.EXPAND|wx.ALIGN_RIGHT)
            self.SetSizer(sizer)
    
    class MyApp(wx.App):
        def OnInit(self):
            frame = MyFrame(None, -1, "FlexGridSizer")
            frame.Show(True)
            self.SetTopWindow(frame)
            return True
    if __name__ == "__main__":
        app = MyApp(0)
        app.MainLoop()
    

    We get this: enter image description here

    If, however, you want the button separately in the far right-hand bottom of the screen, you might want to add more than one sizer or choose a different sizer altogether. Such as a GridSizer or a GridBagSizer