如何使用Tkinter绘制Rombus?

时间:2016-12-14 23:36:04

标签: python tkinter

我正在尝试使用tkinter使用定义的Rhombus类绘制菱形。在下面的代码中,我使用了Rectangle类来获取Rhombus。虽然我现在不知道如何使用它来使用Tkinter绘制不同大小的Rhombus es。

这是我的代码:

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

1 个答案:

答案 0 :(得分:1)

要绘制多边形,您需要点 - 即。 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()

application/x-www-form-urlencoded

然后你可以画它

var request = new Request('./time', {
    method: 'POST', 
    body:'number=6626****',
    headers: {
        "Content-Type": "application/x-www-form-urlencoded"
    }
});

screenshot of output