Python tkinter我如何理解那些不说有什么问题的错误?
当我试图运行我的代码时,我得到了很多错误,我甚至不知道这些错误在说什么。这里有一些错误,都只是说代码,没有说错误:我会写下错误信息和我的代码,请告诉我错误是什么,以及我如何修复它,谢谢。我刚开始写这段代码,这就是为什么有这么少的东西,我对Tkinter有点陌生 错误消息。Python tkinter我如何理解那些不说有什么问题的错误?,python,tkinter,Python,Tkinter,当我试图运行我的代码时,我得到了很多错误,我甚至不知道这些错误在说什么。这里有一些错误,都只是说代码,没有说错误:我会写下错误信息和我的代码,请告诉我错误是什么,以及我如何修复它,谢谢。我刚开始写这段代码,这就是为什么有这么少的东西,我对Tkinter有点陌生 错误消息。 File "C:/MathsQuiz/venv/mainmaths.py", line 31, in <module> instance = Starting(root) File "C:/Math
File "C:/MathsQuiz/venv/mainmaths.py", line 31, in <module>
instance = Starting(root)
File "C:/MathsQuiz/venv/mainmaths.py", line 13, in __init__
self.usern = Label(root.frame, text="Please enter a username", font=("16"))
File "C:\Users\marky\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 3143, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Users\marky\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2561, in __init__
BaseWidget._setup(self, master, cnf)
File "C:\Users\marky\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2530, in _setup
self.tk = master.tk
AttributeError: 'function' object has no attribute 'tk'
Process finished with exit code 1
您的代码存在一些问题
root.frame
作为用户名标签的父项,它应该是self.frame
行按钮(self.frame,text=“Continue”,COMMAND=clear1)
,clear1
不是根据python定义的,您需要使用self.clear1`来引用类方法命令作为参数名并不是这样工作的。它应该类似于按钮(self.frame,text=“Continue”,command=self.clear1)
,也可以类似于按钮(self.frame,{command:self.clear1},text=“Continue”)
完整代码:
从tkinter导入*
随机输入
x=random.randint(5,12)
y=random.randint(5,12)
上课时间:
定义初始(自我,主):
self.frame=frame(master,padx=200,pady=200)
self.frame.grid()
self.title=标签(self.frame,text=“多级数学测验”,
字体=(“Helvetica”、“20”、“粗体”))
self.title.grid(行=0,padx=30,pady=30)
self.usern=Label(self.frame,text=“请输入用户名”,font=(“16”))
self.usern.grid(行=1,padx=20,pady=20)
self.userentry=Entry(self.frame,宽度=50)
self.userentry.grid(行=2)
self.usercont=按钮(self.frame,text=“Continue”,command=self.clear1)
self.usercont.grid(行=3)
def clear1(自):
self.frame.destroy()
如果名称=“\uuuuu main\uuuuuuuu”:
root=Tk()
root.title=(“数学测验”)
实例=开始(根)
root.mainloop()
self.usern=Label(root.frame,text=“请输入…
我想问题出在root.frame
中。你的意思是self.frame
吗?如果你想更好地理解它,那么看看你的错误代码的第一行,它是第31行和instance=start(root)
最后显示的是AttributeError:“function”对象没有属性“tk”
如果要实际查明错误,您没有指定整个代码,请尝试提供以下内容:)