简体   繁体   English

使用图形库绘制填充的多边形

[英]Drawing a filled polygon using graphics library

I have to draw an hourglass in PyCharm. 我必须在PyCharm中画一个沙漏。 I tried the following, but for some reason the setFill command doesn't work. 我尝试了以下操作,但是由于某些原因, setFill命令不起作用。 It only works for canvas.drawRect(x, y, width, height). 它仅适用于canvas.drawRect(x, y, width, height). I guess because the program doesn't recognize that the lines are making a triangle and therefore can't fill it. 我猜是因为程序无法识别直线在成三角形,因此无法填充它。

Does anyone have any idea how I can fix it? 有谁知道我该如何解决?

canvas.setFill("blue")
canvas.drawLine(100, 50, 200, 50)
canvas.drawLine(100, 50, 150, 200)
canvas.drawLine(200, 50, 150, 200)

The lines you are tracing do not define a fillable object. 您要跟踪的行未定义可填充对象。 You should define objects that support the setFill() method: 您应该定义支持setFill()方法的对象:

from graphics import *

def main():
    win = GraphWin("My Canvas", 300, 300)

    poly_points = [Point(100, 50), Point(200, 50), Point(150, 200)]

    p = Polygon(poly_points)
    p.setFill('red')
    p.draw(win)

    c = Circle(Point(50,50), 10)
    c.setFill('blue')
    c.draw(win)

    win.getMouse() # Pause to view result
    win.close()    # Close window when done

main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM