在没有窗口/GUI 的情况下运行 Pygame [英] Run Pygame without a window / GUI

查看:78
本文介绍了在没有窗口/GUI 的情况下运行 Pygame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在不创建 pygame 窗口、界面或 GUI 的情况下运行 pygame?我想使用某些 pygame 功能,但我不想弹出 GUI.

Is it possible to run pygame without creating a pygame window, surface or GUI? I want to utilize certain pygame functions, but I don't want a GUI popping up.

例如,除非我在 pygame 中设置了一个窗口,否则此功能将无法工作.

For example, this function won't work unless I have set up a window within pygame.

running = True

def mainloop():
while True:

    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ) or \
        ( event.type == pygame.KEYDOWN and \
        ( event.key == pygame.K_ESCAPE) ):
            running = False
            print "quit"
            pygame.quit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
                print "working"

推荐答案

DrevanTronder 是正确的,但他没有正确回答问题,因为您问的是如何在没有 GUI 的情况下在 pygame 中执行事件,如您的示例编码所示.

DrevanTronder is correct, but he does not answer the question correctly, as you are asking how to do events in pygame without a GUI, as seen in your example coding.

这是不可能的.运行 Pygame 程序时,它仅在选择 Pygame 窗口时检测事件.如果没有 Pygame 窗口,则没有任何选择,因此事件将不起作用.以下内容直接从 Pygame 文档中提取:

This is not possible. When running a Pygame program, it only detects events if the Pygame window is selected. If there is no Pygame window, there is nothing to select, so events won't work. The following is pulled directly from the Pygame documentation:

如果显示器尚未初始化且未设置视频模式,则事件队列将无法真正工作."https://www.pygame.org/docs/ref/event.html

"If the display has not been initialized and a video mode not set, the event queue will not really work." https://www.pygame.org/docs/ref/event.html

但是,当然,如果您不是专门讨论事件处理,那么 DrevanTronder 是正确的;您需要做的就是使用import pygame"导入模块并使用pygame.init()"对其进行初始化.

But, of course, if you are not talking about event handling specifically, then DrevanTronder is correct; all you need to do is import the module with "import pygame" and initialize it with "pygame.init()".

希望这会有所帮助!

这篇关于在没有窗口/GUI 的情况下运行 Pygame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆