为什么Python会这么说';App&x27;在使用Tkinter时未定义?
我试图为一个学校项目创建一个GUI,但它一直说Tkinter的一个必修步骤没有定义 顺便说一句,我已经进口了Tkinter 这是我的代码:为什么Python会这么说';App&x27;在使用Tkinter时未定义?,python,user-interface,tkinter,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) con
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()
我的问题:
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()行上方。在创建类之前调用该类。将类App放在App=App()行上方。请问回溯是什么?另外,我已经编辑了您的代码,但现在看起来您有一个嵌套的类。我是否犯了错误,或者这是您的原始代码?您需要在类定义之后添加
app=app()
。请注意,回溯的内容可能重复?此外,我已经编辑了您的代码,但现在看起来您有一个嵌套类。我是否犯了错误,或者这是您的原始代码?您需要将app=app()。