Search code examples
pythonwxpythonstatusbar

edit StatusBar from ChildClass


I created a statusBar in my wxApp, but this statusbar is not editable from another class:

import wx

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, 
                          "Pyramid App",size=(800,600),style= wx.SYSTEM_MENU | wx.CAPTION | wx.MINIMIZE_BOX | wx.CLOSE_BOX)

        self.SetBackgroundColour((232,232,232))
        self.p = wx.Panel(self,size=(800,600),pos=(0,0))
        self.PageThree = pageThree(self)
        self.statusBar = self.CreateStatusBar()
        self.statusBar.SetFieldsCount(2)
        self.statusBar.SetStatusText('status bar from MainFrame', 0)
        self.ChangeStatusBar('foo',1)

    def ChangeStatusBar(self,txt,field):
        self.statusBar.SetStatusText(txt,field)

class pageThree(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent,size=(800,525))
        self.myparent=parent
        self.pageThree=wx.Panel(self,size=(800,525))
        wx.StaticText(self.pageThree, -1, 'this is page three', (20,20))
        #self.myparent.ChangeStatusBar('bar',1)

if __name__ == "__main__":
    app = wx.App()
    MainFrame().Show()
    app.MainLoop()

When I uncomment the last line of my childclass then I get this error: MainFrame object has no attribut statusBar. How can I edit the statusBar text from child class?


Solution

  • you are instansiating it before self.statusBar exists

    self.PageThree = pageThree(self) #now self.statusBar does not exists ...
    self.statusBar = self.CreateStatusBar()  
    

    ... switch the order

    self.statusBar = self.CreateStatusBar()  
    self.PageThree = pageThree(self) #now self.statusBar exists ...