Thread: Menu events

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Location
    Missouri
    Posts
    59
    Rep Power
    9

    Menu events


    I'm using this code from this website , and it isn't working.

    Code:
    from wxPython.wx import *
    
    ID_ABOUT = 101
    ID_EXIT  = 102
    
    #frame class
    class MyFrame(wxFrame):
        def __init__(self, parent, id, title):
            wxFrame.__init__(self, parent, id, title, wxDefaultPosition, wxSize(200, 200))
            
            #create statusBar
            self.CreateStatusBar()
            self.SetStatusText("This is the status bar")
            
            #create fileMenu
            menu = wxMenu()
            menu.Append(ID_ABOUT, "&About", "More info about this program")
            menu.AppendSeparator()
            menu.Append(ID_EXIT, "E&xit", "Terminate the program")
            
            #create menuBar
            menuBar = wxMenuBar()
            menuBar.Append(menu, "&File")
            self.SetMenuBar(menuBar)
    
    		#add event listeners
    		EVT_MENU(self, ID_ABOUT, self.OnAbout)
    		EVT_MENU(self, ID_EXIT, self.OnExit)
    	
    	def OnAbout(self, event):
    		dlg = wxMessageDialog(self, "This sample program shows off\n"
    							 "frames, status bars, menus, andn this\n" 
    							 "Message Dialog", "About Me", wxOK | wxICON_INFORMATION)
    		dlg.ShowModal()
    		dlg.Destroy()
    	
    	def OnExit(self, event):
    		self.Close(true)
    
    #window class
    class MyApp(wxApp):
    	def OnInit(self):
    		frame = MyFrame(NULL, -1, "Hello World")
    		frame.Show(true)
    		self.SetTopWindow(frame)
    		return true
    
    #create instance
    app = MyApp(0)
    app.MainLoop()
    Here is there error i'm getting:
    File "C:/Documents and Settings/Owner/My Documents/Python Programs/firstWindow.py", line 27
    EVT_MENU(self, ID_ABOUT, self.OnAbout)
    ^
    SyntaxError: invalid syntax
    I have no idea what the problem is. Someone please help!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    Code:
           menuBar = wxMenuBar()
            menuBar.Append(menu, "&File")
            self.SetMenuBar(menuBar)
    
    		#add event listeners
    		EVT_MENU(self, ID_ABOUT, self.OnAbout)
    		EVT_MENU(self, ID_EXIT, self.OnExit)
    Why did you add the extra indentation?
    That might be where you start looking...
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  4. #3
  5. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    That is often the problem with getting code from a web page. The code had all the wrong indentation which was caused by mixing spaces and tabs. I have tidied it up and it should now work for you:
    python Code:
    from wxPython.wx import *
     
    ID_ABOUT = 101
    ID_EXIT  = 102
     
    #frame class
    class MyFrame(wxFrame):
        def __init__(self, parent, id, title):
            wxFrame.__init__(self, parent, id, title, wxDefaultPosition, wxSize(200, 200))
     
            #create statusBar
            self.CreateStatusBar()
            self.SetStatusText("This is the status bar")
     
            #create fileMenu
            menu = wxMenu()
            menu.Append(ID_ABOUT, "&About", "More info about this program")
            menu.AppendSeparator()
            menu.Append(ID_EXIT, "E&xit", "Terminate the program")
     
            #create menuBar
            menuBar = wxMenuBar()
            menuBar.Append(menu, "&File")
            self.SetMenuBar(menuBar)
     
            #add event listeners
            EVT_MENU(self, ID_ABOUT, self.OnAbout)
            EVT_MENU(self, ID_EXIT, self.OnExit)
     
        def OnAbout(self, event):
            dlg = wxMessageDialog(self, "This sample program shows off\n"
                                                     "frames, status bars, menus, andn this\n" 
                                                     "Message Dialog", "About Me", wxOK | wxICON_INFORMATION)
            dlg.ShowModal()
            dlg.Destroy()
     
        def OnExit(self, event):
            self.Close(true)
     
    #window class
    class MyApp(wxApp):
        def OnInit(self):
            frame = MyFrame(NULL, -1, "Hello World")
            frame.Show(true)
            self.SetTopWindow(frame)
            return true
     
    #create instance
    app = MyApp(0)
    app.MainLoop()


    Tabs and indentation is always a good place to start checking when you paste code

    Have fun

    grim

    Comments on this post

    • Nathan87 agrees : Thanks a lot that fixed it!!!!
    Last edited by Grim Archon; March 7th, 2006 at 12:38 AM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Location
    Missouri
    Posts
    59
    Rep Power
    9
    ok, I swear in my editor, it did not look like it was tabed in again, but thanks!

    now i've fixed the tab problem, but i'm getting another error
    Code:
    from wxPython.wx import *
    
    ID_ABOUT = 101
    ID_EXIT  = 102
    
    #frame class
    class MyFrame(wxFrame):
        def __init__(self, parent, id, title):
            wxFrame.__init__(self, parent, id, title, wxDefaultPosition, wxSize(200, 200))
            
            #create statusBar
            self.CreateStatusBar()
            self.SetStatusText("This is the status bar")
            
            #create fileMenu
            menu = wxMenu()
            menu.Append(ID_ABOUT, "&About", "More info about this program")
            menu.AppendSeparator()
            menu.Append(ID_EXIT, "E&xit", "Terminate the program")
            
            #create menuBar
            menuBar = wxMenuBar()
            menuBar.Append(menu, "&File")
            self.SetMenuBar(menuBar)
            
            #add event listeners
            EVT_MENU(self, ID_ABOUT, self.OnAbout)
            EVT_MENU(self, ID_EXIT, self.OnExit)
    
    	def OnAbout(self, event):
    		dlg = wxMessageDialog(self, "This sample program shows off\n"
    							 "frames, status bars, menus, andn this\n" 
    							 "Message Dialog", "About Me", wxOK | wxICON_INFORMATION)
    		dlg.ShowModal()
    		dlg.Destroy()
    	
    	def OnExit(self, event):
    		self.Close(true)
    
    #window class
    class MyApp(wxApp):
    	def OnInit(self):
    		frame = MyFrame(NULL, -1, "Hello World")
    		frame.Show(true)
    		self.SetTopWindow(frame)
    		return true
    
    #create instance
    app = MyApp(0)
    app.MainLoop()
    Now, I'm getting this error:
    Traceback (most recent call last):
    File "C:/Documents and Settings/Owner/My Documents/Python Programs/firstWindow.py", line 49, in ?
    app = MyApp(0)
    File "C:\Program Files\Python\Python24\lib\site-packages\wx-2.6-msw-ansi\wx\_core.py", line 7668, in __init__
    self._BootstrapApp()
    File "C:\Program Files\Python\Python24\lib\site-packages\wx-2.6-msw-ansi\wx\_core.py", line 7320, in _BootstrapApp
    return _core_.PyApp__BootstrapApp(*args, **kwargs)
    File "C:/Documents and Settings/Owner/My Documents/Python Programs/firstWindow.py", line 43, in OnInit
    frame = MyFrame(NULL, -1, "Hello World")
    File "C:/Documents and Settings/Owner/My Documents/Python Programs/firstWindow.py", line 27, in __init__
    EVT_MENU(self, ID_ABOUT, self.OnAbout)
    AttributeError: 'MyFrame' object has no attribute 'OnAbout'
    please help!
  8. #5
  9. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    See my post about mxing tabs and spaces above.

    grim
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Location
    Missouri
    Posts
    59
    Rep Power
    9
    Never mind! Grim Archon I don't know what you did differently than I did, because I checked the tabs as well, but whatever you did it works! Thanks a lot.
  12. #7
  13. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    No problem

    Just for reference.. When writing python you should decide to use tabs or spaces. - in IDLE select all the code and chose the edit->untabify menu option. Then you check that each method/function is at the right indentation level to make sense.

    The OnAbout method was indented too deep, It was defined in the init method and not as part of the class - so the program could not find self.OnAbout.

    grim
  14. #8
  15. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    610
    Rep Power
    65

    Smile


    Welcome to the Python Indentation Spaghetti Salad (PISS). Mixed tabs and spaces are the Achilles heel of Python. A fair number of code samples on the internet suffer from this. Grim is right, I would stick with the defacto standard of 4 spaces and avoid tabs religiously, since you never know to how many spaces they are set on someone elses editor!

    "Mixed tabs and spaces are the Achilles heel of Python." should be added to the Zen of Python!
    Code:
    # show the Zen of Python
    
    import this

    Comments on this post

    • Grim Archon agrees
    Last edited by Dietrich; March 7th, 2006 at 01:26 AM.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Location
    Missouri
    Posts
    59
    Rep Power
    9
    Thanks everyone, that helped a lot. I think my problem was that this was my first program and I was experimenting with editors and one interpreted tabs as spaces and the other interpreted them as well...tabs. I'll make sure not to make that mistake again!

    Thanks again for all the help!

IMN logo majestic logo threadwatch logo seochat tools logo