pygame 中是否有最小化和最大化屏幕的事件处理选项? [英] Is there an event handling option to minimize and maximize screen in pygame?
问题描述
是否有任何事件处理程序可以像退出屏幕一样最小化或最大化屏幕?
Is there any event handler to minimize or maximize screen like there is to quit screen?
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
推荐答案
Pygame 在窗口最小化/图标化或最大化时将 pygame.ACTIVEEVENT
s 添加到事件队列中.您可以检查 if event.gain == 1 and event.state == 6:
和 if event.gain == 0 and event.state == 6:
以查看如果窗口最大化或最小化.唯一的问题是当窗口获得输入焦点时 event.gain == 1 and event.state == 6
也是 True
.
Pygame adds pygame.ACTIVEEVENT
s to the event queue when the window is minimized/iconified or maximized. You can check if event.gain == 1 and event.state == 6:
and if event.gain == 0 and event.state == 6:
to see if the window was maximized or minimized. The only problem is that event.gain == 1 and event.state == 6
is also True
when the window gains input focus.
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()
.
If you want to minimize/iconify the window with a key press, you can call pygame.display.iconify()
.
这篇关于pygame 中是否有最小化和最大化屏幕的事件处理选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!