why does my class act like this

+2 Zachary Ireland · July 8, 2015

please help

from tkinter import *
import math

class Point:
x = 1
y = 1

class Line:
a = Point()
b = Point()
M = 1
B = 0

def set_up(self, a, b):
"""
:type a: Point
:type b: Point
"""
self.a=a
self.b=b
self.M = (a.y-b.y)/(a.x-b.x)
self.B=a.y-self.M*a.x

def draw (self,g):
g.create_line(a.x,a.y,b.x,b.y)

def draw1 (self,g,a,b):
g.create_line(a.x,a.y,b.x,b.y)


class Square:# class in question
centerPoint = Point()
a = Point()
b = Point()
c = Point()
d = Point()
size =20
r=45

def __init__(self, x, y, size, r):
self.centerPoint.x=x
self.centerPoint.y=y
self.size=size
self.r=r
self.construct()


def draw(self,g):
f=Line()
f.draw1(g,self.a,self.b)
f.draw1(g,self.b,self.c)
f.draw1(g,self.c,self.d)
f.draw1(g,self.d,self.a)

def construct(self):
self.a.x = math.sin(math.radians(self.r))*(self.size)+self.centerPoint.x
self.a.y = math.cos(math.radians(self.r))*(self.size)+self.centerPoint.y

self.b.x = math.sin(math.radians(self.r+90))*(self.size)+self.centerPoint.x
self.b.y = math.cos(math.radians(self.r+90))*(self.size)+self.centerPoint.y

self.c.x = math.sin(math.radians(self.r+180))*(self.size)+self.centerPoint.x
self.c.y = math.cos(math.radians(self.r+180))*(self.size)+self.centerPoint.y

self.d.x = math.sin(math.radians(self.r+270))*(self.size)+self.centerPoint.x
self.d.y = math.cos(math.radians(self.r+270))*(self.size)+self.centerPoint.y


master = Tk()

one= Square(100,200,10,0)
two= Square(10,200,10,90)

g = Canvas(master, width=400, height=400)
g.pack()

one.draw(g)
two.draw(g)

master.mainloop()


i created two square objects, but that doesn't seem to matter they seem to affect each other.
i made a comment on the class i believe is the problem.

i'm new to python so i'm sure it's something simple i just can't see it. 

Post a Reply

Replies

Oldest  Newest  Rating
0 Zachary Ireland · July 9, 2015
oh come on please help i'm stuck
0 Otto Von Chesterfield · July 9, 2015
Class attributes not defined in their methods are class-wide. This means that the first square's 'a' attribute is equal to the second square's "a" attribute.

Here's a snippet of the code to fix it:

# -snip-

class Square:

    #Remove the code here...

    def __init__(self, x, y, size, r):
        self.centerPoint = Point()
        self.centerPoint.x=x
        self.centerPoint.y=y
        self.size=size
        self.r=r

        #And replace it here!
        self.a = Point()
        self.b = Point()
        self.c = Point()
        self.d = Point()
       
        self.construct()

    #More code..

# -snip-

I changed the code and ran it on my computer and it looks like it works!
Tell me if it works for you!
0 Zachary Ireland · July 10, 2015
Thank you so much!, i guess i'm just used to programming in java.
0 Otto Von Chesterfield · July 12, 2015
 Made that mistake before.

So many syntax errors only with a few semicolons...
  • 1

Python

107,335 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator