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