我怎么能把我的圆圈半填成白色呢?

2024-10-05 10:12:27 发布

您现在位置:Python中文网/ 问答频道 /正文

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)

这是我的代码,输出应该如下所示:

enter image description here


Tags: 代码infordefredwinpointdraw
2条回答

下面是我对@JaredWindover的解决方案进行的修改。首先,尽可能多的图形对象设置在嵌套循环之前完成,利用Zelle的clone()方法。其次,它修复了一个很难在小范围内看到的缺陷,即圆的一半轮廓是黑色而不是红色。最后,与Jared的解决方案和OP的代码不同,它是可伸缩的:

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

刚刚测试过这个,对我很有效。你知道吗

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

我们先画满的圆,然后画一半的矩形,然后画出轮廓的圆来恢复轮廓。if检查我们在哪一列。你知道吗

相关问题 更多 >

    热门问题