简体   繁体   English

与平台无关的实时画布绘制?

[英]Platform-independent, real-time canvas drawing?

I want to draw on a canvas in real time, possibly set individual pixels, draw geometric shapes like rectangles, and insert images. 我想在画布上实时绘制,可能设置单个像素,绘制矩形等几何形状,并插入图像。

This is what I want it to look like: 这是我想要的样子:

from greatgui import Window, Canvas
from time import sleep

width = 640
height = 480

w = Window("My title", (width,height))
c = Canvas((width, height))

w.add(c)

i = 0

while True:
    c.putpixel((i, i), color=(255,255,255))
    i += 1
    w.update()
    sleep(0.1)

Anything of greater complexity or setup cost is unacceptable. 任何更大的复杂性或设置成本都是不可接受的。 Am I down on my luck? 我很不走运吗?

I have not found a single example of a GUI framework that doesn't require me to: 我还没有找到一个不需要我执行以下操作的GUI框架示例:

  • create dozens of lines of boilerplate code and platform checks 创建数十行样板代码和平台检查
  • install anything extra (especially compile something by hand!), except through pip 除了通过pip以外,还需要安装其他任何东西(尤其是手工编译!)。
  • add some callback hook into some framework 在一些框架中添加一些回调钩子
  • transfer control to any mainloop at all 将控制权转移到任何主循环
  • renounce all the comfort of Pillow's ImageDraw module 放弃Pillow的ImageDraw模块的所有舒适性

So far https://www.pygame.org seems to be a good choice: 到目前为止, https://www.pygame.org似乎是一个不错的选择:

import pygame
from time import sleep

pygame.init()
pygame.display.set_caption("My title")

screen = pygame.display.set_mode((640,480))

background_color = (255, 255, 255)

i = 0
running = True
while running:
    screen.fill(background_color)

    pygame.draw.rect(screen, (255, 0, 0), pygame.Rect(i, i, 40, 30))
    i += 1

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    pygame.display.flip()
    sleep(0.1)

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

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