0

A few things first:

  1. I am using Windows 10 Home edition
  2. I am using Python 3.7
  3. I am using pygame 1.9.4
  4. My IDE is Visual Studio Code, with IDLE as the backup

I am currently designing a GUI using pygame. NOTE: the code is NOT done yet.

When I run a debug session in VS Code, it (mostly) works as expected, but when I try to click on the start button, pygame does not respond and shows not responding.

I've also noticed this with other pygame scripts I've made, where the pygame window freezes when clicked or moved.

Any help would be appreciated.

Here's the code:

# Import modules
import sys, pygame, time, math
from time import sleep
from PIL import Image

# Display background image
image = 'asdf.png'
change = 2
img = Image.open('asdf.png')
width = img.width * change
height = img.height * change
print(width)
print(height)
screen = pygame.display.set_mode((width,height))
background = pygame.image.load(image).convert()
newscreen = pygame.transform.scale(background, (width, height))
screen.blit(newscreen, (0,0))
pygame.display.update()

# start button
pygame.draw.rect(newscreen, (255,120,0), pygame.Rect(width/4,height-height/4, width/2, height/7))
screen.blit(newscreen, (0,0))
pygame.display.update()
pygame.init()
myFont = pygame.font.SysFont("Times New Roman", 100)
text = myFont.render("START", False, (0, 0, 0))
screen.blit(text, (width/4+8,height-height/4-10))
pygame.display.update()
pygame.image.save(newscreen, 'background.png')
pygame.image.save(text, 'starttext.png')

# i button
pygame.draw.rect(newscreen, (255,0,120), pygame.Rect(width - 50, 10, 40,40))
screen.blit(newscreen,(0,0))
pygame.display.update()
myFont = pygame.font.SysFont("Times New Roman", 25)
ibutton = myFont.render("i", False, (0, 0, 0))
screen.blit(ibutton, (width-32,17))
pygame.display.update()

# Mouse click
while True:
    left,right,center = pygame.mouse.get_pressed()
    if left == True:
        if event.type == MOUSEBUTTONUP:
            x,y = pygame.mouse.get_pos()
            if ((width/4) <= x <= ((width/4) + (width/2))) and ((height-height/4) <= y <= ((height-height/4) + height/76)):
                #move to next screen
                break

time.sleep(5)

This is NOT similar to the linked problem because my program is for a GUI, and it requires mouse click events.

Clemence Yi
  • 13
  • 1
  • 4
  • Possible duplicate of [Pygame window not responding after a few seconds](https://stackoverflow.com/questions/20165492/pygame-window-not-responding-after-a-few-seconds) – skrx Jul 31 '18 at 16:55
  • Not a duplicate, different use case – Clemence Yi Jul 31 '18 at 17:07
  • You’re running an infinite loop that never reads from the event queue or updated the display. So it’s not responsive, which is why Windows tells you it’s not responsive. – abarnert Jul 31 '18 at 17:10
  • It is a duplicate. You have to make a call to one of the [`pygame.event`](https://www.pygame.org/docs/ref/event.html) functions each frame or the window will become unresponsive. – skrx Jul 31 '18 at 17:11
  • Thx 4 all of your answer! I guess my next question would now be how to properly read from the event queue? – Clemence Yi Jul 31 '18 at 17:26

1 Answers1

0

This answer by Ted Klein Bergamn explains why the window becomes unresponsive: https://stackoverflow.com/a/42719689/6220679

This is how pygame.event.get should be used:

running = True
while running:
    for event in pygame.event.get():
        # This lets you quit by pressing the X button of the window.
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONUP:
            if event.button == 1:  # 1 = left mouse button, 2 = middle, 3 = right.
                print('mouse up')
                x,y = pygame.mouse.get_pos()
                if ((width/4) <= x <= ((width/4) + (width/2))) and ((height-height/4) <= y <= ((height-height/4) + height/76)):
                    #move to next screen
                    running = False

In your case (for a simple GUI app) pygame.event.wait would probably be a good choice (it lets the program sleep while no event is in the queue).

running = True
while running:
    event = pygame.event.wait()
    if event.type == pygame.MOUSEBUTTONUP:
        if event.button == 1:
            print('mouse up')
            x,y = pygame.mouse.get_pos()
            if ((width/4) <= x <= ((width/4) + (width/2))) and ((height-height/4) <= y <= ((height-height/4) + height/76)):
                #move to next screen
                running = False
skrx
  • 19,980
  • 5
  • 34
  • 48
  • Thank you so much! – Clemence Yi Jul 31 '18 at 17:48
  • You're welcome! If you have more issues, I recommend checking out some resources like [Program Arcade Games](http://programarcadegames.com/) first. I also recommend adding a [`pygame.time.Clock`](http://www.pygame.org/docs/ref/time.html#pygame.time.Clock) instance and calling `clock.tick(FPS)` each frame if you choose the `pygame.event.get` solution. – skrx Jul 31 '18 at 17:58