简体   繁体   中英

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:

  • create dozens of lines of boilerplate code and platform checks
  • install anything extra (especially compile something by hand!), except through pip
  • add some callback hook into some framework
  • transfer control to any mainloop at all
  • renounce all the comfort of Pillow's ImageDraw module

So far https://www.pygame.org seems to be a good choice:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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