AttributeError:'tkapp'对象没有属性'drawRectangle'

时间:2014-01-05 20:54:25

标签: python oop tkinter attributeerror

我一直试图让我的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")

非常感谢所有输入。

1 个答案:

答案 0 :(得分:0)

错误消息是自解释的。当它显示AttributeError: 'tkapp' object has no attribute 'drawRectangle'时,表示您尝试tkapp.drawRectangletkapp.drawRectangle(...),但tkapp没有名为drawRectangle的属性或方法。

由于您的代码未显示您创建tkapp的位置或创建方式,或者您调用drawRectangle的位置,因此我们无法知道问题的根源是什么。最有可能的是以下之一:

  1. tkapp不是您认为的
  2. 您有拼写错误,打算致电drawLine而不是drawRectangle
  3. 您打算实施drawRectangle,但没有