Sunday, 15 February 2015

A simple example for adding and removing buttons in wxpython -


I am using wxpython.

I want to solve a simple problem for a GUI "add" a button, which when pressed creates a new button called "Remove", you can press "Add" as many times as you want. And many removed buttons are created and added to the panel What do I want when you press one of the Remove buttons, the button is removed and removed from the panel.

The problem occurs when you bind function to a button with:

self.Bind (wx.EVT_BUTTON, self.remove_function, button_name)

You can not give any argument to the given function, which buttons to remove (or can you?)

I wrote about this topic a few years ago:

The following example though Can help:

  import wx ############################### ################################ ######## Class MyPanel (wx.Panel): "" " "" "# --------------------------- ------------------- ------------------------ def __init __ (self, parents): "" "constructor" "wx.panel.__ init __ (self, parents) themselves. Bitian = 1 Atmkman_saijr = wx.boxSizer (wx.VERTICAL) add_btn = wx.Button (own label = add '') add_btn Bain (wx.EVT_BUTTON, self.add_button) self.main_sizer.Add ( Add_btn, 0, wx.CENTER | wx.ALL, 5) self.SetSizer (self.main_sizer) # ---------- ----------------- --------------------------------- ---------- def add_button (auto, event) : "" "" New_btn = wx.button (auto, label = "% s Remove% self.btns) new_btn.bind (wx. EVT_ button, self .remove_button) self.btns + = 1 self.main_sizer.Add (new_btn, 0, wx.CENTER | wx.ALL, 5) self.main_sizer.Layout () # --------- - ------------------------------------------------- - ---------- def remove_button (self, event): "" "" "" Bitian = event.GetEventObject () print "% s button Deleting"% btn.GetLabel () self .main_sizer.Hide (Bitian) self.main_sizer.Remove (Bitian) self.main_sizer.Layout () ############################ ###################################### Class MyFrame (wx.Frame): "" "" "" # --------------------------------------------- - ------------------------ def __init __ (self): "" "constructor" "wx.frame .__ init __ (self, someone No, title = "button") panel = MyPanel (self) self.Show () if __name__ == '__main__': app = Wx.App (wrong) frame = myFrame () app.MainLoop ()  

You can also pass the button object using a lambda, inline function or functools:

And here's a few additional information about several widget bindings for the same event handler:

  • < / Li>

No comments:

Post a Comment