我有一些未完成的代码,因为我陷入了Traceback
错误:
Traceback (most recent call last):
File "D:/Documents/Python/Code Check/Code Check.py", line 40, in <module>
root = codeCheck()
File "D:/Documents/Python/Code Check/Code Check.py", line 10, in __init__
main = mainFrame(self, padding=(60, 30))
File "D:/Documents/Python/Code Check/Code Check.py", line 30, in __init__
checkCodeButton = ttk.Button(self, text="Check Code", command=self.checkCode)
AttributeError: 'mainFrame' object has no attribute 'checkCode'
这是我的代码:
import tkinter as tk
from tkinter import ttk
class codeCheck(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.title("Code Check")
main = mainFrame(self, padding=(60, 30))
main.grid()
class mainFrame(ttk.Frame):
def __init__(self, container, **kwargs):
super().__init__(container, **kwargs)
def checkCode(self, *args):
print("hi")
self.inputedCode = tk.StringVar()
self.code = "123"
enterCodeLabel = ttk.Label(self, text="Code: ")
codeEntry = ttk.Entry(self, textvariable=self.inputedCode)
enterCodeLabel.grid(row=0, column=0)
codeEntry.grid(row=0, column=1)
showCodeButton = ttk.Button(self, text="Show Code")
checkCodeButton = ttk.Button(self, text="Check Code", command=self.checkCode)
showCodeButton.grid(row=1, column=0)
checkCodeButton.grid(row=1, column=1)
root = codeCheck()
root.mainloop()
我正在学习Udemy课程,并且几乎完全复制了脚本。我是python tkinter的新手,我被卡住了。我做错了什么?
答案 0 :(得分:2)
从checkCode()
构造函数中删除mainframe
函数。
import tkinter as tk
from tkinter import ttk
class codeCheck(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.title("Code Check")
main = mainFrame(self, padding=(60, 30))
main.grid()
class mainFrame(ttk.Frame):
def __init__(self, container, **kwargs):
super().__init__(container, **kwargs)
self.inputedCode = tk.StringVar()
self.code = "123"
enterCodeLabel = ttk.Label(self, text="Code: ")
codeEntry = ttk.Entry(self, textvariable=self.inputedCode)
enterCodeLabel.grid(row=0, column=0)
codeEntry.grid(row=0, column=1)
showCodeButton = ttk.Button(self, text="Show Code")
checkCodeButton = ttk.Button(self, text="Check Code", command=self.checkCode)
showCodeButton.grid(row=1, column=0)
checkCodeButton.grid(row=1, column=1)
def checkCode(self, *args): # place this function here out of init
print("hi")
root = codeCheck()
root.mainloop()