我使用多处理来显示pygame
中mss
个模块的屏幕截图.但是,hello消息会显示三次.
我想知道这是否会分散表演的注意力.当我关闭pygame
屏幕时,控制台也会继续运行.这是我的代码:
from mss import mss
from multiprocessing import Process, Queue
import pygame
import pygame.display
import pygame.image
import pygame.time
import pygame.font
import pygame.event
from pygame.locals import *
from numpy import asarray
from cv2 import resize, cvtColor, COLOR_BGRA2BGR
SCR_SIZE = (640, 480)
def grabber(queue: Queue):
global SCR_SIZE
with mss() as sct:
while True:
queue.put(cvtColor(resize(asarray(sct.grab(sct.monitors[1])), SCR_SIZE), COLOR_BGRA2BGR).tobytes())
def displayer(queue: Queue):
global SCR_SIZE
pygame.init()
SCR = pygame.display.set_mode(SCR_SIZE, DOUBLEBUF)
SCR.set_alpha(None)
clock = pygame.time.Clock()
FONT_COMIC = pygame.font.SysFont('Cambria Math', 20)
isGameRunning = True
while isGameRunning:
for EVENT in pygame.event.get():
if EVENT.type == pygame.QUIT:
isGameRunning = False
clock.tick(60)
currentFrame = queue.get()
if currentFrame is not None:
SCR.blit(pygame.image.frombuffer(currentFrame, SCR_SIZE, 'BGR'), (0,0))
else:
break
SCR.blit(FONT_COMIC.render('FPS:'+str(clock.get_fps())[:5], False, (0,255,0)),(10,10))
pygame.display.update()
if __name__ == "__main__":
queue = Queue()
Process(target=grabber, args=(queue,)).start()
Process(target=displayer, args=(queue,)).start()
因此,如果您运行此功能,它将运行得非常完美,但会显示三次社区消息:
pygame 2.0.1 (SDL 2.0.14, Python 3.9.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
pygame 2.0.1 (SDL 2.0.14, Python 3.9.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
pygame 2.0.1 (SDL 2.0.14, Python 3.9.5)
Hello from the pygame community. https://www.pygame.org/contribute.html