python self正在表演

时间:2015-11-19 20:47:04

标签: python-3.x self

嘿,我在运行时遇到错误,这与自己有关。这是我得到的错误:

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()

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

1 个答案:

答案 0 :(得分:1)

通常,当您创建类draw的实例时,会自动为您调用初始化例程draw.__init__,并隐式添加self参数,因此您不会; t明确指定它。这是正常情况:

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

......事实上,这似乎就是你所假设的。您的错误是明确地调用构造函数draw.__init__ 。当您想要这样做时, 某些情况,例如在初始化子类的实例时。但这些往往是更复杂/专业的案例。 如果你这样做,那么你需要在你的通话中明确提供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实例的属性。这意味着在self.canvas = canvas的主体中分配__init__,然后在其他方法中将其称为self.canvas而不仅仅是canvas(忘记最后一部分是一个非常常见的错误我)。