Adding a widget using a button - wxPython

I am trying to create something like a category bar in Wordpress using wxPython.

I'm trying to figure out how to add a widget when a user clicks a button (for example, "Add a new category")

Here is my code:

import wx

class MainWindow(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(300,200))

        self.panel = wx.Panel(self, -1)

        button = wx.Button(self.panel,-1,"Button")

        self.vbox = wx.BoxSizer(wx.VERTICAL)
        self.vbox.Add(button)

        add_btn = wx.Button(self.panel,-1,"Add")
        add_btn.Bind(wx.EVT_BUTTON, self.add)

        hbox = wx.BoxSizer(wx.HORIZONTAL)
        hbox.Add(add_btn)

        main_vbox = wx.BoxSizer(wx.VERTICAL)
        main_vbox.Add(self.vbox)
        main_vbox.Add(hbox)

        self.panel.SetSizer(main_vbox)

        self.Centre()
        self.Show(True)

    def add(self,event):
        self.vbox.Add((wx.Button(self.panel,-1,"Button")))

if __name__ == "__main__":
    app = wx.App()
    MainWindow(None, -1, 'Add a Button')
    app.MainLoop()

My problem is that the button is added on top of the previous button. I'm quite surprised by this, because if I remove the argument of eventthe function add(), and then call it in the method __init__, self.add()it works fine. But this does not help me, because I need to add widgets when the user clicks a button.

Any help is greatly appreciated.

0
source share
1 answer

self.panel.Layout() . ( ), .

0

All Articles