I have problem with bitmap button, I want t odestroy it, but it´s still showing after i click on wbtn, it just prints Destroyed. Here is code:
import wx
class GameFrame(wx.Frame):
def __init__(self,parent,id):
self.shutdown_showing = False
wx.Frame.__init__(self, parent, id, "Project - Map", size=(860, 640))
wx.Frame.CenterOnScreen(self)
self.SetBackgroundColour("green")
self.wndwsbtn = wx.Bitmap("Images/wlogo.png")
self.wbtn = wx.StaticBitmap(self, -1, self.wndwsbtn)
self.wbtn.SetPosition((2, 585))
self.wbtn.Bind(wx.EVT_LEFT_DOWN, self.wstart)
self.wbtn.SetToolTip(wx.ToolTip("Start"))
def wstart(self, event):
if self.shutdown_showing:
self.shutdownbtn.Destroy()
self.Layout()
print "Destroyed"
self.shutdown_showing = False
if not self.shutdown_showing:
self.shutdownbtn = wx.Bitmap("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."))
self.shutdown_showing = True
def wexit(self, event):
print "Exit"
if __name__=='__main__':
app=wx.App(redirect=False)
frame=GameFrame(parent=None,id=-1)
frame.Show()
app.MainLoop()
I think my destroy code isn´t wrong, so i really don´t know why it´s still showing button and print Deystroyed if i click on wbtn. Please help somebody!
As per my understanding of your question you want to destroy/remove the self.wstdwnbtn
when you click on the wbtn
.
Well my approach is to create the self.wstdwnbtn
on the wbtn
. This can be done by following line:
self.wstdwnbtn = wx.StaticBitmap(self.wbtn, -1, self.shutdownbtn)
Because in your code
self.wstdwnbtn = wx.StaticBitmap(self.wbtn, -1, self.shutdownbtn)
you cannot click the self.wstdwnbtn
. Clicking on self.wstdwnbtn
doesn't produced 'Exit' in your case. But my I fixed that too.
More over I found an other logical problem in your code. You are using a variable self.shutdown_showing
to determine when to destroy the self.wstdwnbtn
. Your current logic is as below:
If
self.shutdown_showing
is True, then destroyself.wstdwnbtn
and setself.shutdown_showing
= FalseIf
self.shutdown_showing
is False, then you are again creating aself.wstdwnbtn
and setself.shutdown_showing
= True
So, initially the self.shutdown_showing
= False, so you simply create self.wstdwnbtn
. then you set self.shutdown_showing
= True. So, next time when you click on self.wbtn
the wstart()
is called. Now as the self.shutdown_showing
= True, so due to your logic it destroys the self.wstdwnbtn
and set the self.shutdown_showing
= False. On next line you have if not self.shutdown_showing:
so as the self.shutdown_showing
= False you again create a self.wstdwnbtn
immediately, so you never see the self.wstdwnbtn
getting destroyed.
Solution:
I think you wanted to use if-Else statement. I have fixed your code for all these problems. Now you'll see how the self.wstdwnbtn
gets destroyed and created again.
import wx
class GameFrame(wx.Frame):
def __init__(self,parent,id):
self.shutdown_showing = False
wx.Frame.__init__(self, parent, id, "Project - Map", size=(860, 640))
wx.Frame.CenterOnScreen(self)
self.SetBackgroundColour("green")
self.wndwsbtn = wx.Bitmap("Your Image")
self.wbtn = wx.StaticBitmap(self, -1, self.wndwsbtn)
self.wbtn.SetPosition((2, 585))
self.wbtn.Bind(wx.EVT_LEFT_DOWN, self.wstart)
self.wbtn.SetToolTip(wx.ToolTip("Start"))
def wstart(self, event):
if self.shutdown_showing:
self.wstdwnbtn.Destroy()# Destroy self.wstdwnbtn
self.Layout()
print "Destroyed"
self.shutdown_showing = False
# if not self.shutdown_showing: This was wrong.
else:
self.shutdownbtn = wx.Bitmap("Your image")
# Create self.wstdwnbtn on self.wbtn instead.
self.wstdwnbtn = wx.StaticBitmap(self.wbtn, -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."))
self.shutdown_showing = True
def wexit(self, event):
print "Exit"
if __name__=='__main__':
app=wx.App(redirect=False)
frame=GameFrame(parent=None,id=-1)
frame.Show()
app.MainLoop()
I hope it was helpful.