pygame无法正确显示图标

2024-09-28 01:32:13 发布

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

图标不显示,仅在关闭半秒后显示:

screen = pygame.display.set_mode((1600, 900)) 
pygame.display.set_caption(‘Elizabeth2’) 
icon = pygame.image.load('reindeer.png') 
pygame.display.set_icon(icon) 

Tags: imagepngmodedisplayloadscreenpygame图标
1条回答
网友
1楼 · 发布于 2024-09-28 01:32:13

^{}

[...] 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文件的目录不同

文件的名称和路径可以通过^{}获得。当前工作目录可以通过^{}获取,也可以通过^{}更改

import os

sourceFileDir = os.path.dirname(os.path.abspath(__file__))
os.chdir(sourceFileDir)

另一种解决方案是找到绝对路径。 如果图像相对于python文件的文件夹(甚至在同一文件夹中),则可以获取该文件的目录并连接(^{})图像文件名。e、 g:

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)

相关问题 更多 >

    热门问题