Hello Guys, I have problem with Bindinging Event to menu. Every time I run this on IDLE (F5). I get always Attribute error that self have no attribute OnPrint. What's wrong with the Code????

# SMD Inc since 2003.py

import wx
ID_ABOUT = 100
ID_PRINT = 101
ID_EXIT  =102

class MainWindow(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(500, 350))
        self.CreateStatusBar()
        self.SetStatusText("Displays program Information")
        
        menubar = wx.MenuBar()
        # File menu
        filemenu = wx.Menu()
        filemenu.Append(ID_PRINT, "&Print", "Print the current Window")
        filemenu.Append(ID_EXIT, "E&xit", "Exit the program")
        menubar.Append(filemenu, "&File")
        # Help Menu
        helpmenu = wx.Menu()
        helpmenu.Append(ID_ABOUT, "&File", "About this program")
        menubar.Append(helpmenu, "He&lp")

        # Attach and set Menubar on Frame
        self.SetMenuBar(menubar)

        # Binding Events
        self.Bind(wx.EVT_MENU, self.OnPrint, ID_PRINT)      
        
       

        self.Centre()
        self.Show(True)

        # Setting methods
        def OnPrint(self, event):
            self.close()
            dia = wx.MessageDialog(self, "Thank you!", "About the program", wx.OK)
            dia.ShowModal()
            dia.Destroy()      
        
app = wx.App()
MainWindow(None, -1, "Elijah's SMD Inc. since 2003")
app.MainLoop()

Steve

It looks like the def OnPrint() code is indented too far. It's usually at the same indentation level as the __init__() method.

Thanks BearofNH for your help. Do you mean the sama indentation with

self.Show(True)

? If that is what you say, then that is how the code is. Still don't know what is wrong my dear!!

Steve

No, it should have the same indentation as def __init__() . As it is, your function OnPrint is defined in the scope of the __init__ function, and does not exist outside this scope.

Can you rewite the code and put here so that I will understand what you are saying! By the way thanks for response
Steve

Can you rewite the code and put here so that I will understand what you are saying! By the way thanks for response
Steve

# SMD Inc since 2003.py

import wx
ID_ABOUT = 100
ID_PRINT = 101
ID_EXIT  =102

class MainWindow(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(500, 350))
        self.CreateStatusBar()
        self.SetStatusText("Displays program Information")
        
        menubar = wx.MenuBar()
        # File menu
        filemenu = wx.Menu()
        filemenu.Append(ID_PRINT, "&Print", "Print the current Window")
        filemenu.Append(ID_EXIT, "E&xit", "Exit the program")
        menubar.Append(filemenu, "&File")
        # Help Menu
        helpmenu = wx.Menu()
        helpmenu.Append(ID_ABOUT, "&File", "About this program")
        menubar.Append(helpmenu, "He&lp")

        # Attach and set Menubar on Frame
        self.SetMenuBar(menubar)

        # Binding Events
        self.Bind(wx.EVT_MENU, self.OnPrint, ID_PRINT)      
        
       

        self.Centre()
        self.Show(True)

    # Setting methods
    def OnPrint(self, event):
        self.close()
        dia = wx.MessageDialog(self, "Thank you!", "About the program", wx.OK)
        dia.ShowModal()
        dia.Destroy()      
        
app = wx.App()
MainWindow(None, -1, "Elijah's SMD Inc. since 2003")
app.MainLoop()

I think what Gribouillis thinks is correct.
i am just a new beginner in Python like you.

I have tried to run code as re-written by jice but still get error --- "int object have no attribute GetId"

Still have no clue of what is wrong

bind the item like this, with a keyword argument

self.Bind(wx.EVT_MENU, self.OnPrint, id=ID_PRINT)

Note, with the traceback, you were able to find out that the error happened during this call !

bind the item like this, with a keyword argument

self.Bind(wx.EVT_MENU, self.OnPrint, id=ID_PRINT)

Note, with the traceback, you were able to find out that the error happened during this call !

You have got it Gribouillis!
I replaced ID_PRINT with id=ID_PRINT
Thank you ALL for Good discussion
Apreciation from Jr. MD
God bless you!:D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.