2016-12-14 1 views
0

나는 tkinter를 사용하여 정의 된 Rhombus 클래스를 사용하여 Rhombus를 그리려하고있다. 내 코드에서 Rectangle 클래스를 사용하여 Rhombus.Although을 얻었지만 Tkinter를 사용하여 다른 크기의 마름모를 그릴 때 어떻게 사용할 수 있는지 알지 못합니다. - 즉,어떻게 Tkinter를 사용하여 Rombus를 그릴 수 있습니까?

from math import pi, sin, radians 
from tkinter import * 


class TwoDObject: 
    """Generic class for two dimensional geometric objects. 

    i assume that each 2D object has a unique anchor point (e.g., its center 
    of mass). 

    The class has a counter that keeps track how many objects have been 
    created. 

    Attributes: 
     x (float): x-value of the anchor point. 
     y (float): y-value of the anchor point. 

    Class Attributes: 
     counter (int): Counter for the objects generated. 

    Examples: 

     >>> a = TwoDObject() 
     >>> pos = a.x, a.y 
     >>> pos 
     (0.0, 0.0) 
     >>> repr(a) 
     'TwoDObject(x=0.0, y=0.0)' 
     >>> b = TwoDObject(x=0.0, y=0.0) 
     >>> a == b 
     False 

    """ 
    counter = 0 

    def __init__(self, x=0.0, y=0.0): 
     """Init TwoDObject objects. 

     Args: 
      x (float, optional): x-value of the anchor point. 
       Defaults to 0.0. 
      y (float, optional): y-value of the anchor point. 
       Defaults to 0.0. 

     """ 
     self.x = x 
     self.y = y 
     TwoDObject.counter += 1 

    def __repr__(self): 
     return "TwoDObject(x=%s, y=%s)" % (self.x, self.y) 


class Circle(TwoDObject): 
    """Circles. 

    New Attributes: 
     radius (float): Radius of the circle. 

    Examples: 

     >>> a = Circle(radius=2.0) 
     >>> a 
     Circle(x=0.0, y=0.0, radius=2.0) 
     >>> a.area() # doctest: +ELLIPSIS 
     12.566... 

    """ 

    def __init__(self, x=0.0, y=0.0, radius=1.0): 
     """Init Circle objects. 

     Args: 
      x (float, optional): x-value of the anchor point. 
       Defaults to 0.0. 
      y (float, optional): y-value of the anchor point. 
       Defaults to 0.0. 
      radius (float, optional): Radius of the circle. 
       Defaults to 0.0. 

     """ 
     self.radius = radius 
     super().__init__(x, y) 

    def area(self): 
     """Calculate the area of the circle.""" 
     return pi * (self.radius ** 2) 

    def change_size(self, percent): 
     """Change the size of the circle. 

     Operation does not change the anchor. 

     Args: 
      percent (float): Factor by which we change the size (in percent). 

     """ 
     self.radius *= (percent/100.0) 

    def __repr__(self): 
     return "Circle(x=%s, y=%s, radius=%s)" % (self.x, self.y, self.radius) 


class Rectangle(TwoDObject): 
    """Axis-aligned rectangles. 

    New Attributes: 
     heigth (float): Height of the rectangle. 
     width (float): Width of the rectangle. 

    Examples: 

     >>> a = Rectangle(height=2.0, width=4.0) 
     >>> a.stretch_height(200) 
     >>> a 
     Rectangle(x=0.0, y=0.0, height=4.0, width=4.0) 

    """ 

    def __init__(self, x=0.0, y=0.0, height=1.0, width=1.0): 
     """Init Rectangle objects. 

     Args: 
      x (float, optional): x-value of the anchor point. 
       Defaults to 0.0. 
      y (float, optional): y-value of the anchor point. 
       Defaults to 0.0. 
      height (float, optional): Height of the rectangle. 
       Defaults to 1.0. 
      width (float, optional): Width of the rectangle. 
       Defaults to 1.0. 

     """ 
     self.height = height 
     self.width = width 
     super().__init__(x, y) 

    def area(self): 
     """Calculate the area of the rectangle.""" 
     return self.height * self.width 

    def change_size(self, percent): 
     """Change the size of the rectangle. 

     Operation does not change the anchor. 

     Args: 
      percent (float): Factor by which we change the size (in percent). 

     """ 
     self.height *= (percent/100.0) 
     self.width *= (percent/100.0) 

    def stretch_height(self, percent): 
     """Stretch height of the rectangle. 

     Operation does not change the anchor. 

     Args: 
      percent (float): Factor by which we change the height (in percent). 

     """ 
     self.height *= (percent/100.0) 

    def stretch_width(self, percent): 
     """Stretch width of the rectangle. 

     See Also: 
      stretch_height 

     """ 
     self.width *= (percent/100.0) 

    def __repr__(self): 
     return ("Rectangle(x=%s, y=%s, height=%s, width=%s)" % 
       (self.x, self.y, self.height, self.width)) 


class Square(Rectangle): 
    """Axis-aligned squares. 

    Examples: 

     >>> a = Square(side=3.1) 
     >>> a.stretch_width(200) 
     >>> a 
     Square(x=0.0, y=0.0, side=6.2) 

    """ 

    def __init__(self, x=0.0, y=0.0, side=1.0): 
     """Init Square objects. 

     Args: 
      x (float, optional): x-value of the anchor point. 
       Defaults to 0.0. 
      y (float, optional): y-value of the anchor point. 
       Defaults to 0.0. 
      side (float, optional): Length of one side of the square. 
       Defaults to 1.0. 

     """ 
     super().__init__(x, y, side, side) 

    def change_size(self, percent): 
     super().change_size(percent) 

    stretch_height = change_size 
    stretch_width = change_size 

    def __repr__(self): 
     return "Square(x=%s, y=%s, side=%s)" % (self.x, self.y, self.height) 


class Rhombus(Rectangle): 
    """Axis-aligned squares. 

    Examples: 

     >>> a = Rhombus(theta=44, base=3.1) 
     >>> a 
     Rhombus(theta=44, base=3.1) 
     >>> a.area() 
     6.675666940110965 

    """ 

    def __init__(self,x=0.0,y=0.0, theta=45, base=1.0): 
     self.theta = theta 
     self.base = base 
     super().__init__(x, y, theta) 

    def area(self): 
     return self.base ** 2 * sin(radians(self.theta)) 

    def change_size(self, percent): 
     super().change_size(percent) 

    stretch_height = change_size 
    stretch_width = change_size 

    def __repr__(self): 
     return "Rhombus(theta=%s, base=%s)" % (self.theta, self.base) 


class Window(Frame): 
    def __init__(self, master=None): 
     Frame.__init__(self, master) 
     self.master = master 


def _test(): 
    import doctest 
    doctest.testmod(verbose=True) 


def _test_win(): 
    root = Tk() 
    canvas = Canvas(root, width=200, height=100) 
    canvas.pack(fill=BOTH) 
    obj = Rhombus(base=100,theta=45) 
    o = obj.base 
    t = obj.theta 
    rhom = canvas.create_polygon(, fill="red") 
    mainloop() 

if __name__ == "__main__": 
    _test() 
    _test_win() 
+0

질문에서 제거하십시오 그릴 수 있습니다. 게시 코드에 대한 자세한 내용은 [최소, 완전하고 검증 가능한 예제를 만드는 방법] (http://stackoverflow.com/help/mcve) –

+0

을 참조하십시오. 다각형을 그리려면 점이 필요합니다. [create_polygon] (http : // infohost .nmt.edu/tcc/help/pubs/tkinter/web/create_polygon.html). 마름모는 아무런 포인트가 없으므로 그릴 수 없습니다. – furas

답변

0

당신이 포인트를 필요로 다각형을 그리려면

여기 내 코드입니다. self.polygon

class Rhombus(Rectangle): 

    def __init__(self, x=0.0, y=0.0, theta=45, base=1.0): 
     self.theta = theta 
     self.base = base 
     super().__init__(x, y, theta) 

     a = base * cos(radians(theta)) 
     b = base * sin(radians(theta)) 

     self.polygon = [ 
      x,  y, 
      x+base, y, 
      x+base+a, y+b, 
      x+a,  y+b 
     ] 

다음은`Circle`와`Square` 클래스는 문제의 일부가 아닌 경우는

root = Tk() 

    canvas = Canvas(root, width=200, height=100) 
    canvas.pack(fill=BOTH) 

    obj1 = Rhombus(base=100, theta=45) 
    rhom1 = canvas.create_polygon(obj1.polygon, fill="red") 

    obj2 = Rhombus(x=50, y=15, base=50, theta=60) 
    rhom2 = canvas.create_polygon(obj2.polygon, fill="blue") 

    # rectangle 
    obj3 = Rhombus(y=50, base=50, theta=90) 
    rhom3 = canvas.create_polygon(obj3.polygon, fill="green") 

    root.mainloop() 

enter image description here

관련 문제