错误解释 - 'statInter' 对象没有属性 'tk'
error explanation - 'statInter' object has no attribute 'tk'
我正在尝试在 Python 中创建一个简单的计时器,并打算使用 classes 构建用户界面。我想使用 classes 来初始化用户界面。然后在主体的正文中,我想使用 .grid 和 .configure 方法添加属性。但是当我尝试这样做时,出现错误:'statInter' object has no attribute 'tk'。
我是编程的初学者,但如果我正确理解错误,它的结果是因为我的 statInter(即静态接口)没有继承 .grid 和其他 Button 方法 class。这个对吗?我该如何解决这个错误?我确实继承了 Button class 甚至 Tk class 的属性,但在后一种情况下,我得到了一个无限循环,即超过了最大递归深度。
感谢您的帮助
#This is a simple timer version
from tkinter import *
window = Tk()
window.title('Tea timer')
window.minsize(300,100)
window.resizable(0,0)
class statInter(Button,Entry):
def __init__(self, posx, posy):
self.posx = posx # So the variables inside the class are defined broadly
self.posy = posy
def button(self):
Button(window).grid(row=self.posx, column=self.posy)
def field(self):
Entry(window, width=5)
sth = statInter(1,2)
sth.grid(row=1, column = 2)
window.mainloop()
你得到这个错误的原因是因为你从来没有从你继承的classes(Button
或Entry
)中调用任何一个构造函数。
如果您将 __init__
更改为:
def __init__(self, posx, posy):
Button.__init__(self)
self.posx = posx # So the variables inside the class are defined broadly
self.posy = posy
然后你就不会得到你之前遇到的错误,并且会弹出一点window。在新的 __init__
中,我们显式调用 Button
的构造函数。
与 Java 和其他一些语言不同,默认情况下不调用 super
构造函数。我假设每个从其他 tkinter
class 继承的 class 必须有一个 tk
字段。通过调用您选择的父构造函数,将创建该字段。但是,如果你不调用父构造函数,那么这将不是一个已建立的字段,你将得到你所描述的错误 ('statInter' object has no attribute 'tk').
HTH!
问题是您派生的 StatInter
class (CamelCasing the class name as suggested in PEP 8 - Style Guide for Python Code) 没有初始化它的基础 classes,通常 不会 隐式发生在 Python 中(就像 C++ 中那样)。
为了在 StatInter.__init__()
方法中执行此操作,您将需要知道将包含它的 parent
小部件(除顶级 [=31= 之外的所有小部件) ] 包含在层次结构中)— 因此需要将一个额外的参数传递给派生的 class 的构造函数,以便它可以传递给每个基础 class 构造函数。
您还没有遇到其他问题,但可能很快就会遇到。为避免这种情况,在 button()
和 field()
.
中显式调用基本 class 方法时,您还将显式传递 self
from tkinter import *
window = Tk()
window.title('Tea timer')
window.minsize(300,100)
window.resizable(0,0)
class StatInter(Button, Entry):
def __init__(self, parent, posx, posy): # Added parent argument
Button.__init__(self, parent) # Explicit call to base class
Entry.__init__(self, parent) # Explicit call to base class
self.posx = posx # So the variables inside the class are defined broadly
self.posy = posy
def button(self):
Button.grid(self, row=self.posx, column=self.posy) # Add self
def field(self):
Entry.config(self, width=5) # Add self
sth = StatInter(window, 1, 2) # Add parent argument to call
sth.grid(row=1, column=2)
window.mainloop()
我正在尝试在 Python 中创建一个简单的计时器,并打算使用 classes 构建用户界面。我想使用 classes 来初始化用户界面。然后在主体的正文中,我想使用 .grid 和 .configure 方法添加属性。但是当我尝试这样做时,出现错误:'statInter' object has no attribute 'tk'。
我是编程的初学者,但如果我正确理解错误,它的结果是因为我的 statInter(即静态接口)没有继承 .grid 和其他 Button 方法 class。这个对吗?我该如何解决这个错误?我确实继承了 Button class 甚至 Tk class 的属性,但在后一种情况下,我得到了一个无限循环,即超过了最大递归深度。
感谢您的帮助
#This is a simple timer version
from tkinter import *
window = Tk()
window.title('Tea timer')
window.minsize(300,100)
window.resizable(0,0)
class statInter(Button,Entry):
def __init__(self, posx, posy):
self.posx = posx # So the variables inside the class are defined broadly
self.posy = posy
def button(self):
Button(window).grid(row=self.posx, column=self.posy)
def field(self):
Entry(window, width=5)
sth = statInter(1,2)
sth.grid(row=1, column = 2)
window.mainloop()
你得到这个错误的原因是因为你从来没有从你继承的classes(Button
或Entry
)中调用任何一个构造函数。
如果您将 __init__
更改为:
def __init__(self, posx, posy):
Button.__init__(self)
self.posx = posx # So the variables inside the class are defined broadly
self.posy = posy
然后你就不会得到你之前遇到的错误,并且会弹出一点window。在新的 __init__
中,我们显式调用 Button
的构造函数。
与 Java 和其他一些语言不同,默认情况下不调用 super
构造函数。我假设每个从其他 tkinter
class 继承的 class 必须有一个 tk
字段。通过调用您选择的父构造函数,将创建该字段。但是,如果你不调用父构造函数,那么这将不是一个已建立的字段,你将得到你所描述的错误 ('statInter' object has no attribute 'tk').
HTH!
问题是您派生的 StatInter
class (CamelCasing the class name as suggested in PEP 8 - Style Guide for Python Code) 没有初始化它的基础 classes,通常 不会 隐式发生在 Python 中(就像 C++ 中那样)。
为了在 StatInter.__init__()
方法中执行此操作,您将需要知道将包含它的 parent
小部件(除顶级 [=31= 之外的所有小部件) ] 包含在层次结构中)— 因此需要将一个额外的参数传递给派生的 class 的构造函数,以便它可以传递给每个基础 class 构造函数。
您还没有遇到其他问题,但可能很快就会遇到。为避免这种情况,在 button()
和 field()
.
self
from tkinter import *
window = Tk()
window.title('Tea timer')
window.minsize(300,100)
window.resizable(0,0)
class StatInter(Button, Entry):
def __init__(self, parent, posx, posy): # Added parent argument
Button.__init__(self, parent) # Explicit call to base class
Entry.__init__(self, parent) # Explicit call to base class
self.posx = posx # So the variables inside the class are defined broadly
self.posy = posy
def button(self):
Button.grid(self, row=self.posx, column=self.posy) # Add self
def field(self):
Entry.config(self, width=5) # Add self
sth = StatInter(window, 1, 2) # Add parent argument to call
sth.grid(row=1, column=2)
window.mainloop()