Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'"

StackOverflow https://stackoverflow.com/questions/16680203

Question

complete newbie here to Python as I only started learning the language a few days ago (with no programming experience beforehand).

I'm basically bashing my skull against the desk here, trying to create one menu with a button that will lead you to another menu, which is supposed to replace/hide/destroy the previous menu (either works, so long as the process can be reversed).

What I've come up with so 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 Back a 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 what I can tell, the NextScreen button does not see the intro_screen class, and is therefore unable to show it. But I am clueless as to how to fix this.

Indeed, I have absolutely no idea if this is on the right way to do it. Any help is greatly appreciated

Using Python 2.7

Was it helpful?

Solution

intro_screen is an attribute of MainFrame instances; not of MainMenu instances.

Your MainMenu.__init__() method is passed in a MainFrame instance as parent. I am not certain if self.parent is set by the line wx.Frame.__init__(self, parent=parent), but if it is not, do add self.parent = parent in MainMenu.__init__(.

You can then refer to self.parent on MainMenu instances, and the following should work:

self.parent.intro_screen.Show()

I am not sure why you are setting the current class as an instance attribute:

self.main_menu = MainMenu

and

self.intro_screen = IntroScreen

Instead of self.main_menu.Hide(self) you can just call self.Hide(), the reference to the class is not needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top