Search code examples
python-3.xself

python self is acting up


hey there i keep getting an error when i run this it has something to do with self. this is the error i get:

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

I dont know what could be the problem.


Solution

  • Normally, when you create an instance of a class draw, the initialization routine draw.__init__ is automatically called for you, and the self argument is added implicitly, so you don't specify it explicitly. Here's the normal case:

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

    ...and indeed that's just what you seem to have assumed. Your mistake is calling the constructor draw.__init__ explicitly. There are some situations when you might want to do that, for example while initializing an instance of a subclass. But these tend to be more complicated/specialized cases. If you do it, then you need to supply the self explicitly in your call, and you did not do this. So what happened is that your canvas value got assigned to self, 20 got assigned to canvas (hence the error), and so on.

    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)
    

    I also notice that you've defined a method axies that wants to use the value of canvas. As it's written, that code will be looking for a global variable called canvas. A much safer way to design things is to make that variable an attribute of your draw instance. That means assigning self.canvas = canvas in the body of __init__ and then referring to it as self.canvas rather than just canvas in other methods (forgetting that last part is a really common mistake for me).