I have this program I'm trying to write, a simple application with gui: a menu and a button.
Whenever I go to run it, this is the error I get:

Traceback (most recent call last):
File "/Users/myname/Documents/School/Programming/Window.py", line 27, in <module>
app = Application()
File "/Users/myname/Documents/School/Programming/Window.py", line 14, in __init__
self.createWidgets()
File "/Users/myname/Documents/School/Programming/Window.py", line 17, in createWidgets
self.helloButton = Button (self, text='Hello', command=helloCallBack)
NameError: global name 'helloCallBack' is not defined

This is my code:

#Python 3.1
#!/Library/Frameworks/Python.framework/Versoins/3.1/Resources/Python

from tkinter import *

class Application(Frame):
    helloCallBack = None

    def helloCallBack():
        tkMessageBox.showinfo ("Hello", "Hello, do you like my application?")

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.grid()
        self.createWidgets()
        
    def createWidgets(self):
        self.helloButton = Button (self, text='Hello', command=helloCallBack)
        self.helloButton.pack()
        self.helloButton.grid()
        self.menuFile = Menubutton (self, text='File')
        self.menuFile.grid()
        self.menuFile.hiAgain = Menu (self.menuFile, tearoff = 0)
        self.menuFile["HiAgain"] = self.menuFile.hiAgain
        self.menuFile.hiAgain.add_command(label="Hello", command=helloCallBack)


app = Application()
app.master.title("My Attempt at python")
app.mainloop()

Please help!

I don't speak tkinter, but look at lines 18 and 25. Could be you need to scope the call back funcction? ... , command = [B]Application.[/B]helloCallBack

Thanks for the swift reply, but I'm not sure what happened. I'm only learning this, and have NO IDEA how to interpret this response python gave me:

Traceback (most recent call last):
File "/Users/myname/Documents/School/Programming/Window.py", line 27, in <module>
app = Application()
File "/Users/myname/Documents/School/Programming/Window.py", line 14, in __init__
self.createWidgets()
File "/Users/myname/Documents/School/Programming/Window.py", line 23, in createWidgets
self.menuFile["HiAgain"] = self.menuFile.hiAgain
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/tkinter/__init__.py", line 1200, in __setitem__
self.configure({key: value})
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/tkinter/__init__.py", line 1193, in configure
return self._configure('configure', cnf, kw)
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/tkinter/__init__.py", line 1184, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: unknown option "-HiAgain"

I don't know what happened!! I tried your suggestion, so it may have helped, so this may be in another part of the code, but I just don't know.

MenuButton menuFile has not option hiAgain at line 23. Just read last message from your program and last line from tkinter. You should use small letters and underscores for method names if possible see PEP 8 document.

HelloCallBack is first defined as a variable, then as a function.

class Application(Frame):
    helloCallBack = None
 
    def helloCallBack():
        tkMessageBox.showinfo ("Hello", "Hello, do you like my application?")

Neither is correct, you want it to be a member of the class. Note also that you can not use both grid and pack, so pick one, see the Warning here http://effbot.org/tkinterbook/grid.htm

from tkinter import *
 
class Application(Frame):
##    helloCallBack = None
 
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.grid()
        self.createWidgets()
 
    def helloCallBack(self):
        tkMessageBox.showinfo ("Hello", "Hello, do you like my application?")
 
    def createWidgets(self):
        self.helloButton = Button (self, text='Hello', command=self.helloCallBack)
        self.helloButton.pack()
        self.helloButton.grid()
        self.menuFile = Menubutton (self, text='File')
        self.menuFile.grid()
        self.menuFile.hiAgain = Menu (self.menuFile, tearoff = 0)
        self.menuFile["HiAgain"] = self.menuFile.hiAgain
        self.menuFile.hiAgain.add_command(label="Hello", command=self.helloCallBack)
 
 
app = Application()
app.master.title("My Attempt at python")
app.mainloop()

Thank you all for your help. I have managed to clean up a lot of the code.
Another thing has cropped up. I fear I may be running before I can walk here.
Anyway, here is the code:

#Python 3.1
#!/Library/Frameworks/Python.framework/Versoins/3.1/Resources/Python

from tkinter import *

class Application(Frame):
    
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.grid()
        self.createWidgets()

        
    def hello():
        tkMessageBox.showinfo ("Hello", "Hello, do you like my application?")

        
    def createWidgets(self):
        self.helloButton = Button (self, text='Hello', command=Application.hello)
        self.helloButton.grid()
        self.menubar = Menu (self)
        self.hi = Menu (self.menubar, tearoff = 0)
        self.hi.add_command(label="Hello", command=Application.hello)
        self.menubar.add_cascade(label="File", menu=self.hi)
        #this supposedly shows the menu
        self.config(menu=self.menubar) #This seems to be the problem
        
app = Application()
app.master.title("My Attempt at python")
app.mainloop()

And the error message is:

Traceback (most recent call last):
File "/Users/myname/Documents/School/Programming/Window.py", line 27, in <module>
app = Application()
File "/Users/myname/Documents/School/Programming/Window.py", line 10, in __init__
self.createWidgets()
File "/Users/myname/Documents/School/Programming/Window.py", line 25, in createWidgets
self.config(menu=self.menubar) #This seems to be the problem
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/tkinter/__init__.py", line 1193, in configure
return self._configure('configure', cnf, kw)
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/tkinter/__init__.py", line 1184, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: unknown option "-menu"

Just one thing, could it be I need to do anything different being on a mac?

Yeah, you need do read a litle bit before jumping into it, trying is good, but research also.

Some help

Cheers and Happy coding

Thank you so much, that link was great.
Got it to work now! All i needed to do was change line

self.config(menu=self.menubar)

to

self.master.config(menu=self.menubar)

One little word! Thanks a lot everyone.

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.