Is there an event handling option to minimize and

2019-07-23 05:51发布

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()

1条回答
beautiful°
2楼-- · 2019-07-23 05:52

Pygame adds pygame.ACTIVEEVENTs 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)

If you want to minimize/iconify the window with a key press, you can call pygame.display.iconify().

查看更多
登录 后发表回答