pygame 需要键盘中断来初始化显示 [英] pygame requires keyboard interrupt to init display

查看:58
本文介绍了pygame 需要键盘中断来初始化显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Raspberry Pi 上初始化 pygame,它需要键盘中断才能执行任何操作.这是我的代码:

I'm trying to initialize pygame on a Raspberry Pi and its requiring a keyboard interrupt before it will do anything. Here's my code:

    os.putenv ( "SDL_VIDEODRIVER" , "fbcon" )
    pygame.display.init()    # It hangs here
    screen = pygame.display.set_mode ( ( 1024 , 768 ) )

    pygame.draw.rect ( screen , ( 0 , 255 , 0 ) , ( 15 , 15 , 15 , 15 ) )
    pygame.display.flip()

    keyLoop = True
    while keyLoop:
       for event in pygame.event.get():
          if event.type == pygame.KEYDOWN:
             if event.key == pygame.K_DOWN:
                print ( "Down arrow pressed, exiting" )
                keyLoop = False
                pygame.quit()

我在这里发现了一个类似的问题 Python 程序不会响应直到键盘中断 但它不会让我添加评论,我已经尝试了他们的所有建议,但仍然存在问题.如果我按 CTRL + C,我的图形就会出现,但键盘不起作用.

I found a similar question here Python program won't respond until keyboard interrupt but it wouldn't let me add a comment and I've tried all of their suggestions and still have the problem. If I press CTRL + C then my graphics appear but then the keyboard doesn't work.

谢谢

编辑

我通过完全删除 os.putenv 让它工作.问题实际上出在 config.txt 中的 Pi 设置中.我试图初始化一个比 Pi 的帧缓冲区大的 pygame 显示.确保两者匹配(framebuffer 和 display.set_mode)使其启动正常.

I got it to work by removing os.putenv completely. The problem was actually in the Pi setup in config.txt. I was trying to init a pygame display bigger than the Pi's framebuffer. Making sure the two match (framebuffer and display.set_mode) made it start up just fine.

推荐答案

我遇到了完全相同的问题,但它只会在我的 pygame 代码第二次运行时发生(即第一次在启动时运行,一切正常,但如果你停止程序,然后尝试重新启动它,然后在 pygame.init 或 pygame.display.set_mode 处挂起).

I had exactly the same problem but it would only happen on second running of my pygame code (i.e. first run on boot, everything is OK but if you stop the program and then try to restart it there is then a hang at pygame.init or pygame.display.set_mode).

来自某人或其他人的解决方案对我不起作用(提高键盘中断似乎没有击中主线程,只是分叉线程)但感谢您的想法!生成的工作代码片段如下.

The solution from someone-or-other did not work for me (the raising of the keyboard interrupt did not seem to hit the main thread, just the forked thread) but thanks for the idea! A resulting working code fragment is as follows.

from signal import alarm, signal, SIGALRM, SIGKILL

def init_Pygame():

    # this section is an unbelievable nasty hack - for some reason Pygame
    # needs a keyboardinterrupt to initialise in some limited circs (second time running)
    class Alarm(Exception):
        pass
    def alarm_handler(signum, frame):
        raise Alarm
    signal(SIGALRM, alarm_handler)
    alarm(3)
    try:
        pygame.init()
        DISPLAYSURFACE = pygame.display.set_mode((DISPLAYWIDTH, DISPLAYHEIGHT)) 
        alarm(0)
    except Alarm:
        raise KeyboardInterrupt

    pygame.display.set_caption('Drawing')
    [...rest of initialisation...]

虽然这是一个解决方案,但我不知道是什么导致了这种行为.

Although this is a solution, I don't know what causes the behaviour.

这篇关于pygame 需要键盘中断来初始化显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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