Pygame窗口运行良好,直到点击,然后显示“无响应”

2024-09-29 17:17:44 发布

您现在位置:Python中文网/ 问答频道 /正文

先做几件事:

  1. 我使用的是Windows 10家庭版
  2. 我使用的是python3.7
  3. 我使用的是pygame1.9.4
  4. 我的IDE是visualstudio代码,以IDLE作为备份

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

当我在VS代码中运行调试会话时,它(大部分)按预期工作,但是当我尝试单击start按钮时,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的,它需要鼠标单击事件。在


Tags: 代码imageimporttimepngdisplayupdatewidth
1条回答
网友
1楼 · 发布于 2024-09-29 17:17:44

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

以下是^{}的用法:

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应用程序)^{}可能是一个不错的选择(它允许程序在队列中没有事件的情况下休眠)。在

^{pr2}$

相关问题 更多 >

    热门问题