user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -
complete newbie here python started learning language few days ago (with no programming experience beforehand).
i'm bashing skull against desk here, trying create 1 menu button lead menu, supposed replace/hide/destroy previous menu (either works, long process can reversed).
what i've come far:
import wx class mainframe(wx.frame): def __init__(self): wx.frame.__init__(self, none) self.centre() self.main_menu = mainmenu(self) self.intro_screen = introscreen(self) self.intro_screen.hide() class mainmenu(wx.frame): def __init__(self, parent): wx.frame.__init__(self, parent=parent) self.main_menu = mainmenu panel = wx.panel(self) sizer = wx.boxsizer(wx.vertical) nextscreen = wx.button(panel, label='next screen', size=(150,30)) nextscreen.bind(wx.evt_button, self.nextscreen) sizer.add(nextscreen, 0, wx.center|wx.all, 5) self.show() self.centre() def nextscreen(self, event): self.main_menu.hide(self) self.intro_screen.show() class introscreen(wx.frame): def __init__(self, parent): wx.frame.__init__(self, parent=parent) self.intro_screen = introscreen panel = wx.panel(self) sizer = wx.boxsizer(wx.vertical) gobackscreen = wx.button(panel, label='go screen', size=(150,30)) gobackscreen.bind(wx.evt_button, self.gobackscreen) sizer.add(gobackscreen, 0, wx.center|wx.all, 5) self.show() self.centre() def gobackscreen(self, event): self.intro_screen.hide() self.main_menu.show() if __name__ == "__main__": app = wx.app(false) frame = mainframe() #frame.show() app.mainloop() from can tell, nextscreen button not see intro_screen class, , therefore unable show it. clueless how fix this.
indeed, have absolutely no idea if on right way it. appreciated
using python 2.7
intro_screen attribute of mainframe instances; not of mainmenu instances.
your mainmenu.__init__() method passed in mainframe instance parent. not if self.parent set line wx.frame.__init__(self, parent=parent), if not, add self.parent = parent in mainmenu.__init__(.
you can refer self.parent on mainmenu instances, , following should work:
self.parent.intro_screen.show() i not sure why setting current class instance attribute:
self.main_menu = mainmenu and
self.intro_screen = introscreen instead of self.main_menu.hide(self) can call self.hide(), reference class not needed.
Comments
Post a Comment