Python 我怎么才能把我的圆圈填成白色呢?
这是我的代码,输出应该如下所示:Python 我怎么才能把我的圆圈填成白色呢?,python,zelle-graphics,Python,Zelle Graphics,这是我的代码,输出应该如下所示: 刚刚测试了这个,它对我很有效 def redCircles(): win = GraphWin("Patch2" ,100,100) for x in (10, 30, 50, 70, 90): for y in (10, 30, 50, 70, 90): c = Circle(Point(x,y), 10) c.setFill("red") c.draw(w
刚刚测试了这个,它对我很有效
def redCircles():
win = GraphWin("Patch2" ,100,100)
for x in (10, 30, 50, 70, 90):
for y in (10, 30, 50, 70, 90):
c = Circle(Point(x,y), 10)
c.setFill("red")
c.draw(win)
我们先画满了的圆,然后画过一半的矩形,然后画出轮廓的圆来恢复轮廓。if检查我们在哪一列。下面是我对@JaredWindover的解决方案进行的(臃肿的)修改。首先,在嵌套循环之前尽可能多地设置图形对象,利用Zelle的clone()
方法。第二,它修复了一个在小范围内很难看到的缺陷,圆圈中的一半轮廓是黑色而不是红色。最后,与Jared的解决方案和OP的代码不同,它是可伸缩的:
from graphics import *
def redCircles():
win = GraphWin("Patch2" ,100,100)
for x in (10, 30, 50, 70, 90):
for y in (10, 30, 50, 70, 90):
c = Circle(Point(x,y), 10)
d = Circle(Point(x,y), 10)
if x in (30, 70):
r = Rectangle(Point(x - 10, y), Point(x + 10, y + 10))
else:
r = Rectangle(Point(x - 10, y- 10), Point(x, y + 10))
c.setFill("red")
d.setOutline("red")
r.setFill("white")
r.setOutline('white')
c.draw(win)
r.draw(win)
d.draw(win)
if __name__=='__main__':
redCircles()
不要使用
圆
,而是创建两个多边形
对象,每个对象代表½个圆,一个用红色填充,另一个用白色填充。或者,您可以用红色填充整个圆
,然后创建一个白色多边形
对象,表示要用白色填充的那一半。@martineau这是一个赋值,@gboffi,我应该删除我的答案吗?我刚刚看到你的评论。@JaredWindover这是一个很好的回答,可能已经太晚了。我觉得你不应该…谢谢你让我进一步了解python(y)
from graphics import *
RADIUS = 25
def redCircles(win):
outline_template = Circle(Point(0, 0), RADIUS)
outline_template.setOutline('red')
fill_template = outline_template.clone()
fill_template.setFill('red')
horizontal_template = Rectangle(Point(0, 0), Point(RADIUS * 2, RADIUS))
horizontal_template.setFill('white')
horizontal_template.setOutline('white')
vertical_template = Rectangle(Point(0, 0), Point(RADIUS, RADIUS * 2))
vertical_template.setFill('white')
vertical_template.setOutline('white')
for parity, x in enumerate(range(RADIUS, RADIUS * 10, RADIUS * 2)):
for y in range(RADIUS, RADIUS * 10, RADIUS * 2):
fill = fill_template.clone()
fill.move(x, y)
fill.draw(win)
if parity % 2 == 1:
rectangle = horizontal_template.clone()
rectangle.move(x - RADIUS, y)
else:
rectangle = vertical_template.clone()
rectangle.move(x - RADIUS, y - RADIUS)
rectangle.draw(win)
outline = outline_template.clone()
outline.move(x, y)
outline.draw(win)
if __name__ == '__main__':
win = GraphWin('Patch2', RADIUS * 10, RADIUS * 10)
redCircles(win)
win.getMouse()
win.close()