2016-12-14 4 views
0

私はtkinterを使って定義されたRhombusクラスを使ってRhombusを描画しようとしています。下の私のコードでは、Rectangleクラスを使ってRhombusを取得しました.Tkinterを使って異なるサイズのRhombusを描画するにはどうすればいいのでしょうか? - すなわち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

関連する問題