为什么在使用Tkinter时Python会说未定义“ App”?

时间:2018-12-21 19:37:19

标签: python user-interface tkinter

我正在尝试为学校项目创建GUI,但一直说未定义Tkinter的强制步骤之一。

我已经导入了Tkinter,顺便说一句。

这是我的代码:

from tkinter import *

app = App()


class App(Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init(self, *args, **kwargs)
        container = Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (StartPage, CoachPick):
            frame = F(self, container)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, context):
        frame = self.frames[context]
        frame.tkraise()


class StartPage(Frame):

    def __init__(self, parent, controller):
        Frame.__init__(self, parent)

        text = Label(self, text="Hello. Welcome to the Basketball Game. In this game, you will be trying to draft a team.", fg="black")
        text2 = Label(self, text="As you go on, more directions will be given to you. Enjoy!", fg="black")

        text.pack()
        text2.pack()

        button = Button(self, text="Start", bg="white", fg="black", command=lambda: controller.show_frame(CoachPick))
        button.pack()


def printTextSteve():


    print("Your coach is Steve Kerr.")



def printTextGregg():


    print("Your coach is Gregg Poppovich.")



def printTextBrad():


    print("Your coach is Brad Stevens.")



class CoachPick(Frame):

    def __init__(self, parent, controller):
        Frame.__init__(self, parent)

        text3 = Label(self, text="The first thing you need to do pick a coach. You will have 3 options:", fg="black")
        text3.pack()

        button2 = Button(self, text="Steve Kerr", bg="white", fg="red", command=printTextSteve)
        button2.pack()
        button3 = Button(self, text="Gregg Poppovich", bg="white", fg="red", command=printTextGregg)
        button3.pack()
        button4 = Button(self, text="Brad Stevens", bg="white", fg="red", command=printTextBrad)
        button4.pack()


app.mainloop()

我的问题:

它表示未定义名称“ App”。为什么?

1 个答案:

答案 0 :(得分:2)

创建类之前,请先调用该类。将类App放在app = App()行上方。