Pregunta

I want to check if bitmap button exists and if exists, then destroy it and if it don´t exists, then create it. This is code:

    try:
        shutdownbtn
    except NameError:
        sbex = False
    else:
        sbex = True

    if sbex:
        self.shutdownbtn.Destroy()
        print "Destroyed"

    if not sbex:
        self.shutdownbtn = wx.Bitmap("bin/Images/wstdwn.png")
        self.wstdwnbtn = wx.StaticBitmap(self, -1, self.shutdownbtn)
        self.wstdwnbtn.SetPosition((0, 550))
        self.wstdwnbtn.Bind(wx.EVT_LEFT_DOWN, self.wexit)
        self.wstdwnbtn.SetToolTip(wx.ToolTip("Exit to main menu."))

It creates the bitmap button, but doesn't delete it! Why? I really don't know.

¿Fue útil?

Solución

This is because you're using shutdownbtn not self.shutdownbtn. You will also have to catch an AttributeError, not a NameError.

Otros consejos

You mean something like this:

    try:
        self.shutdown_showing
    except AttributeError:
        self.shutdown_showing = False
    else:
        self.shutdown_showing = True

    if self.shutdown_showing:
        self.shutdownbtn.Destroy()
        print "Destroyed"

    if not self.shutdown_showing:
        self.shutdownbtn = wx.Bitmap("bin/Images/wstdwn.png")
        self.wstdwnbtn = wx.StaticBitmap(self, -1, self.shutdownbtn)
        self.wstdwnbtn.SetPosition((0, 550))
        self.wstdwnbtn.Bind(wx.EVT_LEFT_DOWN, self.wexit)
        self.wstdwnbtn.SetToolTip(wx.ToolTip("Exit to main menu."))

Because it´s still the same...

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top