python - pygame 中是否有事件处理选项来最小化和最大化屏幕?

标签 python python-3.x pygame event-handling

是否有任何事件处理程序可以最小化或最大化屏幕,就像退出屏幕一样?

    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/

相关文章:

python numpy 掩码意味着性能

python - 打印到屏幕时是否需要使用 "+"运算符连接字符串。 {使用版本 3.3.1}

python - Pygame 缩放 Sprite

python 碰撞检测?

python - PyDev:模块的@DynamicAttrs

python - 为什么这个图像在 tkinter 中不显示?

Python ftplib 最佳 block 大小?

python - (psycopg2.OperationalError) 无效 - 操作码

python - 如何在 flask 中使某些字段的形式可选

python - 闲置3秒后如何再次移动一圈?