gpt4 book ai didi

python - 即使声明了类名也未定义

转载 作者:行者123 更新时间:2023-12-01 04:21:48 25 4
gpt4 key购买 nike

我是 python GUI 编程新手,并且阅读了本教程,说明 Tkinter 使用

我尝试了以下代码

import Tkinter

class simpleapp_tk(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent=parent
self.initialize()

def initialize(self):
pass
if __name__=="__main__":
app=simpleapp_tk(None)
app.title('my application')
app.mainloop()

我本以为会出现一个空白窗口,但结果却是这个

 Traceback (most recent call last):
File "C:/Users/vedev/Desktop/python/app1.py", line 3, in <module>
class simpleapp_tk(Tkinter.Tk):
File "C:/Users/vedev/Desktop/python/app1.py", line 12, in simpleapp_tk
app=simpleapp_tk(None)
NameError: name 'simpleapp_tk' is not defined

如何调试它?

最佳答案

这是你的缩进问题。只是不要缩进

if __name__ == "__main__":
app = simpleapp_tk(None)
app.title('my application')
app.mainloop()

在当前代码中,在定义 simpleapp_tk 时执行 if 语句,因此 name 'simpleapp_tk' 未定义

参见Indentation

Leading whitespace (spaces and tabs) at the beginning of a logical line is used to compute the indentation level of the line, which in turn is used to determine the grouping of statements.

关于python - 即使声明了类名也未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33593890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com