python 自己在胡闹

python self is acting up

嘿,当我 运行 时,我总是收到错误消息,这与我自己有关。这是我得到的错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "F:\JACOB\JACOB\Intro to programing\WinPython-64bit-3.3.5.0\python-3.3.5.amd64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 585, in runfile
    execfile(filename, namespace)
  File "F:\JACOB\JACOB\Intro to programing\WinPython-64bit-3.3.5.0\python-3.3.5.amd64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 48, in execfile
    exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
  File "F:/JACOB/JACOB/Pygrams/JJ interprises.py", line 113, in <module>
    draw._init_(canvas,20,40,60,80,80,80,120,140,120,140,20,40,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
  File "F:/JACOB/JACOB/Pygrams/JJ interprises.py", line 63, in _init_
    self.line1 = canvas.create_line(self.x1, self.y1, self.x2, self.y2, fill="black")
AttributeError: 'int' object has no attribute 'create_line'


from BD import btd
from tkinter import *
import graphics
from math import *



class draw:
    def _init_(self,canvas,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12,y13,y14,y15,y16,y17,y18,y19,y20):
        self.x1 = x1
        self.x2 = x2
        self.x3 = x3
        self.x4 = x4
        self.x5 = x5
        self.x6 = x6
        self.x7 = x7
        self.x8 = x8
        self.x9 = x9
        self.x10 = x10
        self.x11 = x11
        self.x12 = x12
        self.x13 = x13
        self.x14 = x14
        self.x15 = x15
        self.x16 = x16
        self.x17 = x17
        self.x18 = x18
        self.x19 = x19
        self.x20 = x20
        self.y1 = y1
        self.y2 = y2
        self.y3 = y3
        self.y4 = y4
        self.y5 = y5
        self.y6 = y6
        self.y7 = y7
        self.y8 = y8
        self.y9 = y9
        self.y10 = y10
        self.y11 = y11
        self.y12 = y12
        self.y13 = y13
        self.y14 = y14
        self.y15 = y15
        self.y16 = y16
        self.y17 = y17
        self.y18 = y18
        self.y19 = y19
        self.y20 = y20
        self.line1 = canvas.create_line(self.x1, self.y1, self.x2, self.y2, fill="black")
        self.line2 = canvas.create_line(self.x3, self.y3, self.x4, self.y4, fill="black")
        self.line3 = canvas.create_line(self.x5, self.y5, self.x6, self.y6, fill="black")
        self.line4 = canvas.create_line(self.x7, self.y7, self.x8, self.y8, fill="black")
        self.line5 = canvas.create_line(self.x9, self.y9, self.x10, self.y10, fill="black")
        self.line6 = canvas.create_line(self.x11, self.y11, self.x12, self.y12, fill="black")
        self.line7 = canvas.create_line(self.x13, self.y13, self.x14, self.y14, fill="black")
        self.line8 = canvas.create_line(self.x15, self.y15, self.x16, self.y16, fill="black")
        self.line9 = canvas.create_line(self.x17, self.y17, self.x18, self.y18, fill="black")
        self.line10 = canvas.create_line(self.x19, self.y19, self.x20, self.y20, fill="black")


    def bts(v,x,y,z,w):
        x = btd(v,x,y,z,w)
        x = radians(x)
        x = tan(x)


    def axies():
        for i in range(2,82):
            canvas.create_line(20 * i, 40, 20 * i, 820)
            canvas_id1 = canvas.create_text(20*i,20)
            canvas.itemconfig(canvas_id1, text=i-1)
        for c in range(2,42):
            canvas.create_line(40, 20 * c, 1620, 20 * c)
            canvas_id2 = canvas.create_text(20, 20*c)
            canvas.itemconfig(canvas_id2, text=c-1)















win = Tk()
win.title("Deed to Drawing")
win.resizable(True,True)
frame = Frame(win, width=600, height=200)
canvas = Canvas(win, bg="gray", width = 700, height = 600)
draw.axies()
canvas.pack(side="bottom", fill="both", expand=True)
frame.pack()
draw._init_(canvas,20,40,60,80,80,80,120,140,120,140,20,40,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
win.mainloop()

我不知道可能是什么问题。

通常,当你创建一个class draw的实例时,会自动为你调用初始化例程draw.__init__,并隐式添加self参数,所以你没有明确指定它。这是正常情况:

d = draw( canvas, 20, 40, ... )

...事实上,这正是您所假设的。您的错误是显式调用构造函数 draw.__init__ 在某些情况下 您可能想要这样做,例如在初始化子class 的实例时。但这些往往是 complicated/specialized 个案例。 如果你这样做了,那么你需要在你的调用中明确地提供self,而你没有这样做。所以发生的事情是你的 canvas 值被分配给 self,20 被分配给 canvas(因此错误),等等。

d = draw( canvas, 20, 40, ... ) # you mean this
draw.__init__( d, canvas, 20, 40, ... )  # not this (which is correct if you're calling the draw initializer on some pre-existing object d)

我还注意到您定义了一个方法 axies,它想要使用 canvas 的值。如其所写,该代码将寻找一个名为 canvas 的全局变量。一种更安全的设计方法是使该变量成为 draw 实例的属性。这意味着在 __init__ 的主体中分配 self.canvas = canvas 然后将其称为 self.canvas 而不是在其他方法中仅 canvas (忘记最后一部分是一个非常常见的错误我)。