turtle
如果您不想安装外部模块,可以使用该模块。我创建了一些有用的功能:
setwindowsize( x,y )
- 将窗口大小设置为 x*y
drawpixel( x, y, (r,g,b), pixelsize)
- 使用 RGB 颜色(元组)将像素绘制到 x:y 坐标,具有像素大小的厚度
showimage()
- 显示图像
import turtle
def setwindowsize(x=640, y=640):
turtle.setup(x, y)
turtle.setworldcoordinates(0,0,x,y)
def drawpixel(x, y, color, pixelsize = 1 ):
turtle.tracer(0, 0)
turtle.colormode(255)
turtle.penup()
turtle.setpos(x*pixelsize,y*pixelsize)
turtle.color(color)
turtle.pendown()
turtle.begin_fill()
for i in range(4):
turtle.forward(pixelsize)
turtle.right(90)
turtle.end_fill()
def showimage():
turtle.hideturtle()
turtle.update()
例子:
200x200 窗口,中心 1 个红色像素
setwindowsize(200, 200)
drawpixel(100, 100, (255,0,0) )
showimage()
![在此处输入图像描述](https://i.stack.imgur.com/2fV7U.png)
30x30 随机颜色。像素大小:10
from random import *
setwindowsize(300,300)
for x in range(30):
for y in range(30):
color = (randint(0,255),randint(0,255),randint(0,255))
drawpixel(x,y,color,10)
showimage()
![在此处输入图像描述](https://i.stack.imgur.com/Dlda5.png)