图标不显示,关闭半秒后才显示:
screen = pygame.display.set_mode((1600, 900))
pygame.display.set_caption(‘Elizabeth2’)
icon = pygame.image.load('reindeer.png')
pygame.display.set_icon(icon)
最佳答案
[...] Some systems do not allow the window icon to change after it has been shown. This function can be called before
pygame.display.set_mode()
to create the icon before the display mode is set.
在pygame.display.set_mode((1600, 900))
之前设置图标:
icon = pygame.image.load('reindeer.png')
pygame.display.set_icon(icon)
screen = pygame.display.set_mode((1600, 900))
pygame.display.set_caption(‘Elizabeth2’)
此外,图标的大小也受到限制:
[...] You can pass any surface, but most systems want a smaller image around 32x32.
如果图标未显示,请尝试使用较小的图标。
确保资源(图像)路径和工作目录正确。
图像文件路径必须相对于当前工作目录。工作目录可能与 python 文件的目录不同。
文件名和路径可以通过__file__
获取。当前工作目录可以通过 os.getcwd()
获取可以通过 os.chdir(path)
进行更改.
import os
sourceFileDir = os.path.dirname(os.path.abspath(__file__))
os.chdir(sourceFileDir)
另一种解决方案是查找绝对路径。
如果图像是相对于python文件的文件夹(甚至在同一文件夹中),那么您可以获取文件的目录并加入( os.path.join()
)图像文件名。例如:
import pygame
import os
# get the directory of this file
sourceFileDir = os.path.dirname(os.path.abspath(__file__))
iconPath = os.path.join(sourceFileDir, 'reindeer.png')
icon = pygame.image.load(iconPath)
pygame.display.set_icon(icon)
关于python - pygame 图标无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65079658/