如何在Python中使用Zelle图形制作半圆?

时间:2014-12-03 03:23:35

标签: python zelle-graphics

如何在Python的Zelle图形包中制作半圆?这段代码让我成了一个圆圈。

balldistance=40;
ball1=Circle(Point(spacing*b+spacing-150,FieldHeight-GroundDepth),ball1);
ball1.setFill("red");
ball1.draw(Field);

1 个答案:

答案 0 :(得分:1)

Zelle图形模块不提供直接绘制半圆(弧)的代码。但是,由于模块是用Python编写的,构建在tkinter上,而tkinter提供了一个弧绘图例程,我们可以添加自己的Arc子类,它继承自Zelle Oval类并实现了弧:

from graphics import *

class Arc(Oval):

    def __init__(self, p1, p2, extent):
        self.extent = extent
        super().__init__(p1, p2)

    def __repr__(self):
        return "Arc({}, {}, {})".format(str(self.p1), str(self.p2), self.extent)

    def clone(self):
        other = Arc(self.p1, self.p2, self.extent)
        other.config = self.config.copy()
        return other

    def _draw(self, canvas, options):
        p1 = self.p1
        p2 = self.p2
        x1, y1 = canvas.toScreen(p1.x, p1.y)
        x2, y2 = canvas.toScreen(p2.x, p2.y)
        options['style'] = tk.CHORD
        options['extent'] = self.extent
        return canvas.create_arc(x1, y1, x2, y2, options)


win = GraphWin("My arc example", 200, 200)

arc = Arc(Point(50, 50), Point(100, 100), 180)
arc.setFill("red")
arc.draw(win)

win.getMouse()
win.close()

<强>输出

enter image description here