Search code examples
pythonwxpython

Confused on how to structure the GUI (wxpython)


I've gone from one book to another, one google search to another and I notice EVERY SINGLE ONE starts the main window in a completely different way.

I don't want to pick up bad habits so can someone please give me the best of these options and why its the better method. Below are all the ways i've seen it done

A)
class iFrame(wx.Frame): def init(....): wx.Frame._init_(...)

B)
class iFrame(wx.Frame): def init(...): super_init_(...)

C)
Then I see some that uses the Panel instead such as
class iPanel(wx.Panel) def init(...): wx.Panel.init(...)

D)
And even more confusing some are using the regular App class of wx
class iApp(wx.App): def OnInit(self): wx.Frame.init(...)

Forgive me if some of my structures are wrong but I'm recalling these off the top of my head, Question again...Which one of these, IF ANY is the best way to structure the GUI. It's hard following tutorials and books when they all do things in diff ways

edit: Sorry if format is not correct, but normally it works...


Solution

  • My favorite way to start wx application development is:

    import wx
    
    class MainWindow(wx.Frame):
        def __init__(self, *args, **kwargs):
            wx.Frame.__init__(self, *args, **kwargs)
    
            self.panel = wx.Panel(self)
            self.button = wx.Button(self.panel, label="Test")
    
            self.sizer = wx.BoxSizer()
            self.sizer.Add(self.button)
    
            self.panel.SetSizerAndFit(self.sizer)  
            self.Show()
    
    app = wx.App(False)
    win = MainWindow(None)
    app.MainLoop()
    

    See also this question, which is related.