繁体   English   中英

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

[英]Why does Python say that 'App' is not defined when I use 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”。 为什么?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM