简体   繁体   中英

Why does Python say that 'App' is not defined when I use Tkinter?

I am trying to create a GUI for a school project, but it keeps saying that one of the mandatory steps for Tkinter isn't defined.

I already imported Tkinter, btw.

THIS IS MY CODE:

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()

MY PROBLEM:

It says that the name 'App' is not defined. WHY?

You call the class before you created it. Put the class App above the app = App() line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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