dots = [[1,1], [2,1], [2,2], [1,2], [0.5,1.5]]
from tkinter import Canvas
c = Canvas(width=750, height=750)
c.pack()
out = []
for x,y in dots:
out += [x*250, y*250]
c.create_polygon(*out, fill='#aaffff')#fill with any color html or name you want, like fill='blue'
c.update()
或者也可以使用:
dots = [[1,1], [2,1], [2,2], [1,2], [0.5,1.5]]
out = []
for x,y in dots:
out.append([x*250, y*250])
import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((750, 750), 0, 32)
pygame.display.set_caption('WindowName')
DISPLAYSURF.fill((255,255,255))#< ; \/ - colours
pygame.draw.polygon(DISPLAYSURF, (0, 255,0), out)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
另外,如果要在窗口上绘制,请使用以下命令:
或者也可以使用:
第一个需要
tkinter
,第二个需要pygame
。第一个加载速度更快,第二个绘制速度更快,如果你把DISPLAYSURF.fill
和pygame.draw.polygon
放进一个不同坐标的循环中,它将比tkinter中相同的东西工作得更好。所以,如果你的多边形是飞行和反弹,使用第二,但如果它只是一个稳定的东西,使用第一。另外,在python2中使用from Tkinter
,而不是from tkinter
。 我已经在raspberrypi3上检查过这个代码,它是有效的。—————————————————————————
关于PIL和PYPLOT方法的更多信息,请参见其他答案:
matplotlib
使用tkinter
,也许matplotlib
更容易使用,但它基本上是更酷的tkinter
窗口。在本例中,
PIL
使用了imagemagick
,这是一个非常好的图像编辑工具如果还需要对图像应用效果,请使用
PIL
。如果需要更难的数学图形,请使用
matplotlib.pyplot
。对于动画,请使用
pygame
。对于任何你不知道的更好的方法,使用
tkinter
。tkinter
初始化很快。pygame
更新很快。pyplot
只是一个几何工具。另一种绘制多边形的方法是:
请注意,您需要安装枕头库。另外,我把你的坐标放大了100倍,这样我们就能在640x 480屏幕上看到多边形。
希望这有帮助。
相关问题 更多 >
编程相关推荐