是否有任何事件处理程序可以最小化或最大化屏幕,就像退出屏幕一样?
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
最佳答案
当窗口最小化/图标化或最大化时,Pygame 将 pygame.ACTIVEEVENT 添加到事件队列中。您可以检查 if event.gain == 1 and event.state == 6:
和 if event.gain == 0 and event.state == 6:
来查看窗口是否最大化或最小化。唯一的问题是,当窗口获得输入焦点时,event.gain == 1 和 event.state == 6
也为 True
。
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.KEYDOWN:
if event.key == pg.K_i:
pg.display.iconify()
elif event.type == pg.ACTIVEEVENT:
if event.gain == 1 and event.state == 6:
print('maximized')
elif event.gain == 0 and event.state == 6:
print('minimized')
screen.fill(BG_COLOR)
pg.display.flip()
clock.tick(60)
如果您想通过按键最小化/图标化窗口,您可以调用pygame.display.iconify()
.
关于python - pygame 中是否有事件处理选项来最小化和最大化屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54049637/