需要一个简单的python错误的帮助

时间:2014-08-04 14:59:58

标签: python user-interface tkinter syntax-error

我试图制作一个非常简单的程序。我做了一个类似于此的工作。但是这个给了我错误。代码中的第二个按钮是错误所在的位置。我不知道什么是错的。我是编程新手。非常感激任何的帮助。

AttributeError: App instance has no attribute 'h_one'

我的代码:

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 个答案:

答案 0 :(得分:2)

问题是您正在尝试在类 init 函数内定义h函数。 尝试支持一个缩进级别的def h _...函数,就像这样。

    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"