0

I'm trying to create something like the categories panel in Wordpress, with wxPython.

What I'm trying to figure out, is how to add a widget when the user clicks a button (like "Add 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, the button gets added on top of the previous button. I'm rather mystified by this, because if I delete the event argument of the add() function, and then call it in the __init__ method, self.add(), it works fine. But that doesn't help me any because I need to add the widgets when the user clicks the button.

Any help is much appreciated.

1 Answer 1

1

Call self.panel.Layout() after adding the button. This function is called automatically when you resize a window with children (try it with your current code), but not when you add widgets to it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.