Python Tkinter OOP按钮导致属性错误I don';我不明白
我有一些未完成的代码,因为我陷入了一个Python Tkinter OOP按钮导致属性错误I don';我不明白,python,tkinter,Python,Tkinter,我有一些未完成的代码,因为我陷入了一个回溯错误: 回溯(最近一次呼叫最后一次): 文件“D:/Documents/Python/Code-Check/Code-Check.py”,第40行,在 root=codeCheck() 文件“D:/Documents/Python/Code-Check/Code-Check.py”,第10行,在初始化中__ main=大型机(自,填充=(60,30)) 文件“D:/Documents/Python/Code-Check/Code-Check.py”,第3
回溯
错误:
回溯(最近一次呼叫最后一次):
文件“D:/Documents/Python/Code-Check/Code-Check.py”,第40行,在
root=codeCheck()
文件“D:/Documents/Python/Code-Check/Code-Check.py”,第10行,在初始化中__
main=大型机(自,填充=(60,30))
文件“D:/Documents/Python/Code-Check/Code-Check.py”,第30行,在初始化中__
checkCodeButton=ttk.Button(self,text=“Check Code”,command=self.checkCode)
AttributeError:“mainFrame”对象没有属性“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()
我正在学习我的课程,并且几乎准确地复制了剧本。我对python tkinter还很陌生,我被卡住了。我做错了什么?从您的大型机构造函数中删除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)
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()