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)

        panel.SetSizer(vs1)

        #but2.Destroy() THIS WORKS

    def destroybut(self, event):

        QuickSilver.panel.but2.Destroy()
        print "button 2 is destroyed"
                

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

Thanks.

Recommended Answers

All 2 Replies

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

Thanks.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.