我一直试图让我的Tkinter包装器(专门用于制作游戏)工作,但它在尝试绘制矩形时不断抛出错误。
回溯:
Traceback (most recent call last):
File "C:\Users\William\Dropbox\IT\Thor\test.py", line 7, in <module>
aRectangle = thorElements.GameElement(pling,rectangleTup=(True,295,195,305,205,"blue"))
File "C:\Users\William\Dropbox\IT\Thor\thorElements.py", line 79, in __init__
self.rectangle = self.area.drawRectangle(self)
File "C:\Python33\lib\tkinter\__init__.py", line 1867, in __getattr__
return getattr(self.tk, attr)
AttributeError: 'tkapp' object has no attribute 'drawRectangle'
与问题相关的代码部分,
class GameElement():
def __init__(self,area,rectangleTup=(False,12,12,32,32,"red")):
self.area = area
self.lineTup = lineTup #Tuple containing all the data needed to create a line
if self.lineTup[0] == True:
self.kind = "Line"
self.xPos = self.lineTup[1]
self.yPos = self.lineTup[2]
self.line = self.area.drawLine(self)
这是将矩形绘制到画布上的实际方法(在管理Canvas小部件的类中),在同一文件的前面:
class Area():
def drawLine(self,line):
topX = line.lineTup[1]
topY = line.lineTup[2]
botX = line.lineTup[3]
botY = line.lineTup[4]
colour = line.lineTup[5]
dashTuple = (line.lineTup[6][0],line.lineTup[6][1])
return self.canvas.create_line(topX,topY,botX,botY,fill=colour,dash=dashTuple)
print("Drew Line")
非常感谢所有输入。
答案 0 :(得分:0)
错误消息是自解释的。当它显示AttributeError: 'tkapp' object has no attribute 'drawRectangle'
时,表示您尝试tkapp.drawRectangle
或tkapp.drawRectangle(...)
,但tkapp
没有名为drawRectangle
的属性或方法。
由于您的代码未显示您创建tkapp
的位置或创建方式,或者您调用drawRectangle
的位置,因此我们无法知道问题的根源是什么。最有可能的是以下之一:
tkapp
不是您认为的drawLine
而不是drawRectangle
,drawRectangle
,但没有