Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

python - Why is my display not responding while waiting for input?

I tried to use python to display image:

import pygame
win = pygame.display.set_mode((500, 500))
DisplayImage("Prologue.jpg", win)

And when it runs, nothing happened. It also happened for

DisplayImage("Streets.jpg", win)

However, when I tried the exact same thing later on in the code, it ran perfectly.

I checked, the picture was in the same folder as the .py file, and I didn't type the name wrong.

The function is:

def DisplayImage(imageName, screen):
    screen.fill((0, 0, 0))
    Image = pygame.image.load(imageName).convert()
    screen_rect = screen.get_rect()
    Image_rect = Image.get_rect().fit(screen_rect)
    Image = pygame.transform.scale(Image, Image_rect.size)
    screen.blit(Image, [0, 0])
    pygame.display.update()

Update: I commented out all of the lines and copy and pasted that line out so it's the only line that runs. It runs perfectly.

Update 2: Found the issue. The reason it doesn't work was that the pygame window was "not responding". I don't know what caused it to not respond, but in one of the test runs I didn't make it show "not responding", and the images were loaded fine. The "not responding" always shows up when I type in my player name, and the function looks like this:

def createName():
    playerName = input("Enter the player name
")
    desiredName = input("Is "+playerName+" the desired name?[1]Yes/[2]No
")
    if desiredName == "1":
        return playerName
    elif desiredName == "2":
        playerName = createName()

Sometimes when I type the player name nothing happens, and the letters only show up after a while. If this happens, the pygame window is bound to not respond.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You cannot use input in the application loop. input waits for an input. While the system is waiting for input, the application loop will halt and the game will not respond.

Use the KEYDOWN event instead of input:

run = True
while run:
    event_list = pygame.event.get()
    for event in event_list:
        if event.type == pygame.QUIT:
            run = False

        if event.type == pygame.KEYDOWN:
            if pygame.key == pygame.K_1:
                # [...]
            if pygame.key == pygame.K_2:
                # [...]

Another option is to get the input in a separate thread.

Minimal example:

import pygame
import threading

pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()

color = "red"
def get_input():
    global color
    color = input('enter color (e.g. blue): ')

input_thread = threading.Thread(target=get_input)
input_thread.start()

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False          

    window_center = window.get_rect().center
    window.fill(0)
    pygame.draw.circle(window, color, window_center, 100)
    pygame.display.flip()

pygame.quit()
exit()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...