I would like to destroy the second button by clicking the first

I have also tried "return but2.Destroy() but everything returns an error message.

Can anyone help?

import wx

class DestroyButton(wx.Frame):

    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, "title")
        panel = wx.Panel(self)

        but1 = wx.Button(panel, label = "click me")
        but2 = wx.Button(panel, label = "destroy me")

        but1.Bind(wx.EVT_BUTTON, self.destroybut)

        vs1 = wx.BoxSizer(wx.VERTICAL)
        vs1.Add(but1, 1, flag = wx.EXPAND)
        vs1.Add(but2, 1, flag = wx.EXPAND)


        #but2.Destroy() THIS WORKS

    def destroybut(self, event):

        print "button 2 is destroyed"

app = wx.App(0)
frame1 = DestroyButton(parent = None, id = -1).Show()


make it so that it's self.but2 in __init__
This way you can access it from other functions of same object. self.but2.destroy() in your destroybut function should work


That leave me with a new problem now which I will try to work on :)

Need the sizer to be reset, which raises an error when I try to reset the sizer because button 2 no longer exists.

This question has already been answered. Start a new discussion instead.