문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top