0

I'm trying to make a very simple program. I have made one similar to this and it worked. however this one is giving me the error. The second button in the code is where the error speaks of. I have no idea whats wrong. I'm new to programming. Any help would be most appreciated.

AttributeError: App instance has no attribute 'h_one'

My code:

from Tkinter import *
import tkMessageBox

class App:

    def __init__(self, master):
        frame = Frame(master)
        frame.pack()


        self.button = Button(
            frame, text="QUIT", fg="red", command=frame.quit
            )
        self.button.pack(side=LEFT,padx=5)


        self.hone = Button(frame, text="Happy #1", command=self.h_one)
        self.hi_there.pack(side=BOTTOM,pady=5)


        self.htwo = Button(frame, text="Happy #2", command=self.h_two)
        self.hi_there.pack(side=BOTTOM,pady=5)


        self.hthree = Button(frame, text="Happy #3", command=self.h_three)
        self.hi_there.pack(side=BOTTOM,pady=5)


        def h_one(self):
            print "1"

        def h_two(self):
            print "2"

        def h_three(self):
            print "3"

frame=Tk()
frame.title("Mad Mike's Happy Tool")
frame.geometry("360x400+200+200")

label0 = StringVar()
label0.set("MMHT")
labelA = Label(frame, textvariable=label0, height = 4)
labelA.pack(side=BOTTOM)





app = App(frame)


frame.mainloop()
frame.destroy()
1
  • 3
    Look at your indentation; see how h_one and the other methods are indented further than the __init__ method? Try to fix that. Commented Aug 4, 2014 at 15:02

1 Answer 1

2

The problem is that you are trying to define your h functions inside of the class init function. Try backing the def h_... functions one indentation level out, like so.

    def __init__(self, master):
        frame = Frame(master)
        frame.pack()


        self.button = Button(
        frame, text="QUIT", fg="red", command=frame.quit)
        self.button.pack(side=LEFT,padx=5)


        self.hone = Button(frame, text="Happy #1", command=self.h_one)
        self.hi_there.pack(side=BOTTOM,pady=5)


        self.htwo = Button(frame, text="Happy #2", command=self.h_two)
        self.hi_there.pack(side=BOTTOM,pady=5)


        self.hthree = Button(frame, text="Happy #3", command=self.h_three)
        self.hi_there.pack(side=BOTTOM,pady=5)


    def h_one(self):
        print "1"

    def h_two(self):
        print "2"

    def h_three(self):
        print "3"
Sign up to request clarification or add additional context in comments.

1 Comment

Yes that was it, thank you both for the fast responses. Sorry it was such a simple question i asked. i just thought it looked right. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.