【问题标题】:Pygame window runs fine until clicked on, then shows "Not responding"Pygame 窗口运行良好,直到点击,然后显示“无响应”
【发布时间】:2018-07-31 16:53:04
【问题描述】:

先说几件事:

  1. 我使用的是 Windows 10 家庭版
  2. 我使用的是 Python 3.7
  3. 我正在使用 pygame 1.9.4
  4. 我的 IDE 是 Visual Studio Code,以 IDLE 作为备份

我目前正在使用 pygame 设计一个 GUI。注意:代码尚未完成。

当我在 VS Code 中运行调试会话时,它(大部分)按预期工作,但是当我尝试单击开始按钮时,pygame 没有响应并显示没有响应。

在我制作的其他 pygame 脚本中,我也注意到了这一点,当点击或移动时,pygame 窗口会冻结。

任何帮助将不胜感激。

代码如下:

# 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)

这与链接问题不同,因为我的程序是用于 GUI 的,它需要鼠标单击事件。

【问题讨论】:

  • 不是重复的,不同的用例
  • 您正在运行一个从不从事件队列中读取或更新显示的无限循环。所以它没有响应,这就是 Windows 告诉你它没有响应的原因。
  • 它是重复的。您必须在每一帧调用pygame.event 函数之一,否则窗口将变得无响应。
  • 谢谢 4 你所有的答案!我想我的下一个问题是如何正确读取事件队列?

标签: python user-interface pygame python-3.7


【解决方案1】:

Ted Klein Bergamn 的这个回答解释了为什么窗口变得没有响应:https://stackoverflow.com/a/42719689/6220679

pygame.event.get 应该这样使用:

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

在您的情况下(对于简单的 GUI 应用程序)pygame.event.wait 可能是一个不错的选择(它让程序在队列中没有事件时休眠)。

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

【讨论】:

  • 非常感谢!
  • 不客气!如果您有更多问题,我建议您先查看一些资源,例如 Program Arcade Games。如果您选择pygame.event.get 解决方案,我还建议添加一个pygame.time.Clock 实例并在每帧调用clock.tick(FPS)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-02
  • 2022-12-23
  • 1970-01-01
  • 1970-01-01
  • 2013-12-08
相关资源
最近更新 更多