2013-06-02 2 views
1

PyGame으로 이미지를 그리려하고 Adafruit에서이 코드를 복사했습니다. 이상하게도 Ctrl-C를 누르기 전에는 아무 것도 화면에 표시되지 않는 것 같습니다. 이때 이미지가 표시됩니다. 그런 다음 Ctrl-C를 다시 누를 때까지 이미지가 화면에 그대로 유지됩니다.키보드 인터럽트가 생길 때까지는 Python 프로그램이 응답하지 않습니다.

역 추적 (마지막으로 가장 최근 통화) : : 다음 다음과 같은 메시지가 얻을
time.sleep에,
파일 "RaspiDisplay.py", 라인 (60) (10)
KeyboardInterrupt

무슨 일 이니? 그건 그렇고, 나는 라즈베리 파이에 ssh를 통해 디스플레이를 0 (내 TV)로 설정하고 있습니다. 에 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) 
+0

strace로 프로그램을 실행하여 대기중인 내용을 확인하십시오. –

+0

글쎄, 난 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, 0xbe90befc) = 0 IOCTL (4 KDGKBENT가 0xbe90befc) = 0 IOCTL (4, KDGKBENT가 0xbe90befc)은 = 0 IOCTL (4 KDGKBENT는 0xbe90befc) = 0 IOCTL (4 KDGKBENT는 0xbe90befc) = 0 –

+0

왜 당신은'시간 잠 들어 (10)'하고 있니? 나는 그 문제가 어디에 있는지를 약 80 % 확신합니다. 전체 프로그램을 10 초 동안 기다리면 파이 게임 이벤트 루프가 10 초 동안 실행될 기회를 얻지 못하게됩니다. 이는 정확히보고있는 것입니다. – abarnert

답변

2

어디서나 이벤트 루프를 실행하지 않습니다. 대신, 모든 것을 초기화하고 10 초 동안 잠을 자고 있습니다. 10 초 동안 코드는 아무 것도하지 않습니다. 왜냐하면 그렇게하기 위해 말한 것이기 때문입니다. 즉, 화면을 업데이트하거나, 마우스 클릭에 응답하거나, 다른 것을 의미하지 않습니다.

은 파이 게임을 구동 할 수있는 몇 가지 방법이 있지만 가장 간단한이 같은 것입니다 :

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를 사용하도록 허용하지 않으므로 ... 당신이 무엇을하려고 했든, 당신은하지 않습니다. 마지막으로, 파이썬에서 루프 용 found 플래그가 필요하지 않습니다. 그냥 else 절을 사용하십시오.

+0

와우, 정말 고마워! –

관련 문제