简体   繁体   English

直到键盘中断,Python程序才会响应

[英]Python program won't respond until keyboard interrupt

I'm trying to draw an image with PyGame, and I copied this code from Adafruit. 我正在尝试使用PyGame绘制图像,并从Adafruit复制了此代码。 It's strange, seems nothing displays on the screen until I Ctrl-C, at which time it shows the image. 奇怪,直到我按Ctrl-C时,屏幕上才显示任何内容,这时它才显示图像。 Then the image stays on the screen until I press Ctrl-C again. 然后图像停留在屏幕上,直到我再次按Ctrl-C。 I then get the following message: 然后,我收到以下消息:

Traceback (most recent call last): 追溯(最近一次通话):
File "RaspiDisplay.py", line 60, in 文件“ RaspiDisplay.py”,第60行,在
time.sleep(10) time.sleep(10)
KeyboardInterrupt 一个KeyboardInterrupt

What's going on? 这是怎么回事? By the way, I'm running this via ssh on a raspberry pi, with Display set to 0 (my TV) If I put a print statement in init , that also doesn't print until I press Ctrl-C. 顺便说一句,我正在树莓派上通过ssh运行此命令,并且Display设置为0(我的电视)。如果我在init中放置了一条print语句,那么在按下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)

You're not running an event loop anywhere. 您不在任何地方运行事件循环。 Instead, you're just initializing everything, and then going to sleep for 10 seconds. 相反,您只是初始化所有内容,然后进入睡眠状态10秒钟。 During that 10 seconds, your code is doing nothing, because that's what you told it to do. 在那10秒钟内,您的代码什么都不做,因为这就是您告诉它要做的。 That means no updating the screen, responding to mouse clicks, or anything else. 这意味着无需更新屏幕,响应鼠标单击或其他任何操作。

There are a few different ways to drive pygame, but the simplest is something like this: 有几种不同的方式来驱动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()

See the tutorial for more information. 有关更多信息,请参见教程


As a side note, your initialization code has a bunch of problems. 附带说明一下,您的初始化代码有很多问题。 You iterate through three drivers, but you only set SDL_VIDEODRIVER once, so you're just trying 'fbcon' three times in a row. 您遍历了三个驱动程序,但是只设置了SDL_VIDEODRIVER一次,因此您只是连续尝试了三次'fbcon' Also, you've got code to detect the X display, but you don't allow pygame/SDL to use X, so… whatever you were trying to do there, you're not doing it. 另外,您已经有了检测X显示的代码,但是您不允许pygame / SDL使用X,因此……无论您在此尝试做什么,都没有这么做。 Finally, you don't need a found flag in Python for loops; 最后,你并不需要found在Python for循环标志; just use an else clause. 只需使用else子句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM