3 Replies - 652 Views - Last Post: 27 November 2012 - 11:39 AM Rate Topic: -----

#1 Pin Head  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 236
  • Joined: 31-July 09

Pythom pygame screen hanging

Posted 27 November 2012 - 10:46 AM

Hi,
I'm new to python my main experience is with java, anyway I am running a piece of code from the python IDE.I run
this code and it works correctly and brings up what I expected to see, the problem is is when I click the close button the x in the corner the screen hangs and then comes unresponsive.

But when I go to the py file and double click it the program runs fine and closes correctly.


#!/usr/bin/env python

background_image_filename = "sushiplate.jpg"
mouse_image_filename = "fugu.png"

import pygame
from pygame.locals import *
from sys import exit

pygame.init()

screen = pygame.display.set_mode(( 640, 480 ), 0, 32 )
pygame.display.set_caption( "Hello World!" )

background = pygame.image.load( background_image_filename ).convert()
mouse_cursor = pygame.image.load( mouse_image_filename ).convert_alpha()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()

    screen.blit( background, ( 0, 0 ) )

    x,y = pygame.mouse.get_pos()
    x-= mouse_cursor.get_width() / 2
    y-= mouse_cursor.get_height() / 2
    screen.blit( mouse_cursor, ( x, y ) )

    pygame.display.update()




Error info

Traceback (most recent call last):
File "C:\Users\Genesis\Desktop\game programming opengl\python game programming\source code\learning code\pygame\hello world\helloWorld.py", line 21, in <module>
exit()
SystemExit
>>>

Is This A Good Question/Topic? 0
  • +

Replies To: Pythom pygame screen hanging

#2 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Pythom pygame screen hanging

Posted 27 November 2012 - 11:17 AM

Does it still hang if you replace exit() with:
pygame.quit()
break





EDIT: Wow, first time I've ever used IDLE. It's pretty awesome. This code works well for me (no errors when quitting -> no hangs)
I commented out the mousecursor and background code because I don't have any images on this VM to test with, but it should be OK.

#!/usr/bin/env python

background_image_filename = "sushiplate.jpg"
mouse_image_filename = "fugu.png"

import pygame
from pygame.locals import *
from sys import exit

pygame.init()

screen = pygame.display.set_mode(( 640, 480 ), 0, 32 )
pygame.display.set_caption( "Hello World!" )

#background = pygame.image.load( background_image_filename ).convert()
#mouse_cursor = pygame.image.load( mouse_image_filename ).convert_alpha()
quitting = False
while True:
    for event in pygame.event.get():
        print "Got event!: ", event
        if event.type == QUIT:
            print "Quitting!"
            quitting = True
            pygame.quit()
            break
            
    if quitting: break
    #screen.blit( "#000000", ( 0, 0 ) )

    x,y = pygame.mouse.get_pos()
    #x-= mouse_cursor.get_width() / 2
    #y-= mouse_cursor.get_height() / 2
    #screen.blit( mouse_cursor, ( x, y ) )

    pygame.display.update()
        


This post has been edited by Python_4_President: 27 November 2012 - 11:28 AM

Was This Post Helpful? 0
  • +
  • -

#3 Pin Head  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 236
  • Joined: 31-July 09

Re: Pythom pygame screen hanging

Posted 27 November 2012 - 11:33 AM

Hi,
Sorry took a bit of time getting back I have tried your recommendation and the code runs perfectly and when closed doesn't hang like before.I suppose I've just got to get use to using python a bit better.Anyway I just want to say thank you for your help.
Was This Post Helpful? 1
  • +
  • -

#4 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Pythom pygame screen hanging

Posted 27 November 2012 - 11:39 AM

I think the issue probably has to do with what sys.exit() does. It'll terminate the thread it's called in, but not ALL threads. Not sure EXACTLY what's going on, but I bet it comes down to something in the background expected you to say goodbye before leaving.

Sometimes if you forget to say goodbye, the host gets really depressed and has a breakdown.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1