I have been working with Python and wxPython for about 5 hours and as distributed as the information is, I seem to be making good progress. However, the following behaviour bugs me. I have no idea where to go. Can anyone explain?
In the following code there are 3 buttons, all with the same parent (an instance of wxPanel)
- btn1 simply closes the frame
- btn2 changes the colour of btn1 twice
- by calling a method of btn1's parent upon a click (that works just fine)
- by calling parent.btn1.SetBackgroundColour("Red") from within def __init__ (that works just fine)
- btn3 tries to change the colour of btn1 twice from within an event method (and fails both times)
- parent.btn1.SetBackgroundColour("Yellow") (gobal name parent not found)
- self.parent.btn1.SetBackgroundColour("Yellow") (instance has no attribute parent)
If I move the btn3 code to the def __init__ method then it works (just like btn2)
And the code:
# demonstrate behaviour
from wxPython.wx import *
from wxPython.lib.buttons import *
···
#----------------------------------------------------------------------
class MyOtherButton(wxGenButton):
def __init__(self, parent, ID):
wxGenButton.__init__(self, parent, ID, "")
self.SetSize(wxSize(150,50))
self.SetPosition(wxPoint(10,110))
EVT_BUTTON(self, self.GetId(), self.OnButton)
def OnButton(self, evt):
print "OnButton3"
parent.btn1.SetBackgroundColour("Yellow")
self.parent.btn1.SetBackgroundColour("Yellow")
#----------------------------------------------------------------------
class MyButton(wxGenButton):
def __init__(self, parent, ID):
wxGenButton.__init__(self, parent, ID, "")
self.SetSize(wxSize(150,50))
self.SetPosition(wxPoint(10,60))
EVT_BUTTON(self, self.GetId(), parent.OnButton2)
parent.btn1.SetBackgroundColour("Red")
#----------------------------------------------------------------------
class MyPanel(wxPanel):
def __init__(self, parent, id=-1,
pos=wxDefaultPosition, size=wxDefaultSize,
style=wxTAB_TRAVERSAL, name="panel"):
wxPanel.__init__(self, parent, id, pos, size, style, name)
self.SetSize(wxSize(500,500))
self.btn1 = wxGenButton(self, -1, "Close",wxPoint(10,10),wxSize(150,50) )
EVT_BUTTON(self, self.btn1.GetId(), parent.OnButton)
self.btn2=MyButton(self,-1)
self.btn3=MyOtherButton(self,-1)
def OnButton2(self, evt):
"""Event handler for the button click."""
print "OnButton2"
self.btn1.SetBackgroundColour("Blue")
self.btn1.Refresh()
#----------------------------------------------------------------------
class MyFrame(wxFrame):
def __init__(self, parent, id, title):
wxFrame.__init__(self, parent, id, title)
self.insideframe = MyPanel(self,-1,pos=wxPoint(500,500))
def OnButton(self, evt):
"""Event handler for the button click."""
print "OnButton"
self.Close()
def OnCloseWindow(self, event):
self.Destroy()
#----------------------------------------------------------------------
class MyApp(wxApp):
def OnInit(self):
frame = MyFrame(NULL, -1, "This is a test")
frame.ShowFullScreen(true, style = wxFULLSCREEN_ALL)
frame.Show(true)
self.SetTopWindow(frame)
return true
#----------------------------------------------------------------------
app = MyApp(0)
app.MainLoop()
Thanks in advance
Mike