Search code examples
wxpython

wx python how to place a button on a frame


I have two buttons on a panel. I want one on the left and one on the right. There must be a simple way but I can't see it (the (400, 0) spacer is just an example; it should work on any panel)

import wx


class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1,
                          'Demonstrate wxPython button')
        self.panel = MainPanel(self)
        self.Fit()
        self.Centre()
        self.Show()


class MainPanel(wx.Panel):
    def __init__(self, frame):
        wx.Panel.__init__(self, frame)

        # Button
        button_sizer = self._button_sizer(frame)

        # Main sizer
        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add((400, 0))
        main_sizer.Add(button_sizer)
        self.SetSizer(main_sizer)
        self.Fit()

    def _button_sizer(self, frame):
        cmd_save = wx.Button(self, wx.ID_SAVE)
        cmd_cancel = wx.Button(self, wx.ID_CANCEL)
        button_sizer = wx.BoxSizer(wx.HORIZONTAL)
        button_sizer.Add(cmd_save)
        button_sizer.Add(cmd_cancel, flag=wx.ALIGN_RIGHT)
        return button_sizer


if __name__ == '__main__':
    screen_app = wx.App()
    main_frame = MainFrame()
    screen_app.MainLoop()

(Sorry about this; StackOverflow want more text)

Lorem ipsum dolor sit amet, porttitor donec, eget dolor tempor imperdiet, dignissim a sit wisi sed, congue sit accumsan aliquam urna fusce. Placerat vel ligula curabitur in nostra nunc, lorem lorem morbi in aliquet. Etiam ultricies ante sed at aenean id, ligula nibh aenean fusce sapien, ipsum felis sapien eu, mollis ut tempus odio suspendisse fames. A massa wisi sed nunc maecenas pretium, massa duis vitae non rhoncus nunc, orci sit maecenas ut, aliquet amet, vitae sit. Quam velit rutrum justo varius felis ligula, ut habitasse mauris, platea nunc. Et tincidunt vel, libero vitae aliquet donec suspendisse arcu, faucibus orci. Praesent mollis diam eu justo, quam diam arcu vel tincidunt. Aliquam eu fusce ipsum, ante turpis sociosqu, quis posuere pellentesque in pellentesque, nulla sit, phasellus lorem inceptos. Sociis ornare quam magnis eget cursus, ut pretium, velit vitae et sit vivamus, mi malesuada at lectus amet amet vitae. Convallis tristique gravida cras aenean, amet risus sem vehicula ac lacinia et, elit ac, magna eget ut.


Solution

  • I have always found the the sizer element proportion is your friend in cases like these. Which defines how much room a certain element should take up in relation to others.
    Here is your code, with the proportion option being used.

    import wx
    class MainFrame(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, None, -1, 'Demonstrate wxPython button')
            self.panel = MainPanel(self)
           # self.Fit()
            self.Centre()
            self.Show()
    
    class MainPanel(wx.Panel):
        def __init__(self, frame):
            wx.Panel.__init__(self, frame)
            # Button
            button_sizer = self._button_sizer(frame)
            # Main sizer
            main_sizer = wx.BoxSizer(wx.VERTICAL)
            main_sizer.Add(button_sizer,proportion=1, flag=wx.EXPAND)
            self.SetSizer(main_sizer)
            self.Fit()
    
        def _button_sizer(self, frame):
            cmd_save = wx.Button(self, wx.ID_SAVE)
            cmd_cancel = wx.Button(self, wx.ID_CANCEL)
            button_sizer = wx.BoxSizer(wx.HORIZONTAL)
            button_sizer.Add(cmd_save)
            button_sizer.Add((-1, -1), proportion=1)
            button_sizer.Add(cmd_cancel)
            return button_sizer
    
    if __name__ == '__main__':
        screen_app = wx.App()
        main_frame = MainFrame()
        screen_app.MainLoop()