【问题标题】:Python program won't respond until keyboard interruptPython程序直到键盘中断才会响应
【发布时间】:2013-06-02 04:35:02
【问题描述】:

我正在尝试使用 PyGame 绘制图像,并且我从 Adafruit 复制了此代码。很奇怪,在我 Ctrl-C 之前屏幕上似乎什么都没有显示,此时它显示了图像。然后图像会一直停留在屏幕上,直到我再次按下 Ctrl-C。然后我收到以下消息:

回溯(最近一次通话最后一次):
文件“RaspiDisplay.py”,第 60 行,在
时间.sleep(10)
键盘中断

发生了什么事?顺便说一句,我在树莓派上通过 ssh 运行它,显示设置为 0(我的电视) Ctrl-C。

import os
import pygame
import time
import random

class pyscope :
    screen = None;

    def __init__(self):
        "Ininitializes a new pygame screen using the framebuffer"
        # Based on "Python GUI in Linux frame buffer"
        # http://www.karoltomala.com/blog/?p=679
        disp_no = os.getenv("DISPLAY")
        if disp_no:
            print "I'm running under X display = {0}".format(disp_no)

        # Check which frame buffer drivers are available
        # Start with fbcon since directfb hangs with composite output
        drivers = ['fbcon', 'directfb', 'svgalib']
        found = False
        for driver in drivers:
            # Make sure that SDL_VIDEODRIVER is set
            if not os.getenv('SDL_VIDEODRIVER'):
                os.putenv('SDL_VIDEODRIVER', driver)
            try:
                pygame.display.init()
            except pygame.error:
                print 'Driver: {0} failed.'.format(driver)
                continue
            found = True
            break

        if not found:
            raise Exception('No suitable video driver found!')

        size = (pygame.display.Info().current_w, pygame.display.Info().current_h)
        print "Framebuffer size: %d x %d" % (size[0], size[1])
        self.screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
        # Clear the screen to start
        self.screen.fill((0, 0, 0))        
        # Initialise font support
        pygame.font.init()
        # Render the screen
        pygame.display.update()

    def __del__(self):
        "Destructor to make sure pygame shuts down, etc."

    def test(self):
        # Fill the screen with red (255, 0, 0)
        red = (255, 0, 0)
        self.screen.fill(red)
        # Update the display
        pygame.display.update()

# Create an instance of the PyScope class
scope = pyscope()
scope.test()
time.sleep(10)

【问题讨论】:

  • 尝试用 strace 运行你的程序,看看它在等待什么。
  • 好吧,我运行了 strace,似乎我的程序卡住了:ioctl(4, KDGKBENT, 0xbe90befc) = 0 ioctl(4, KDGKBENT, 0xbe90befc) = 0 ioctl(4, KDGKBENT, 0xbe90befc ) = 0 ioctl(4, KDGKBENT, 0xbe90befc) = 0 ioctl(4, KDGKBENT, 0xbe90befc) = 0 ioctl(4, KDGKBENT, 0xbe90befc) = 0 ioctl(4, KDGKBENT, 0xbe90befc) = 0 ioctl(4, KDGKBENT, 0xbe99 ) = 0 ioctl(4, KDGKBENT, 0xbe90befc) = 0 ioctl(4, KDGKBENT, 0xbe90befc) = 0 ioctl(4, KDGKBENT, 0xbe90befc) = 0 ioctl(4, KDGKBENT, 0xbe90befc) = 0
  • 你为什么要time.sleep(10)?我大约有 80% 确定这就是您的问题所在。您让整个程序等待 10 秒,这意味着 pygame 事件循环没有机会运行 10 秒,这正是您所看到的。

标签: python pygame


【解决方案1】:

您没有在任何地方运行事件循环。相反,您只是初始化所有内容,然后休眠 10 秒。在这 10 秒内,您的代码什么也不做,因为这是您告诉它要做的。这意味着不会更新屏幕、响应鼠标点击或其他任何事情。

有几种不同的方式来驱动 pygame,但最简单的是这样的:

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()
        # any other event handling you need
    # all the idle-time stuff you want to do each frame
    # usually ending with pygame.display.update() or .flip()

请参阅tutorial 了解更多信息。


附带说明一下,您的初始化代码有很多问题。您遍历三个驱动程序,但您只设置了一次SDL_VIDEODRIVER,所以您只是连续尝试了三次'fbcon'。此外,您有检测 X 显示的代码,但您不允许 pygame/SDL 使用 X,所以……无论您试图在那里做什么,您都没有这样做。最后,在 Python for 循环中不需要 found 标志;只需使用else 子句。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-13
    • 2019-11-11
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    • 2014-08-11
    • 2023-03-16
    • 2013-04-30
    相关资源
    最近更新 更多