简体   繁体   English

pygame 无法正确显示图标

[英]Icons are not displayed properly with pygame

The icon is not displayed, it's only displayed after closing for half a second:图标不显示,关闭半秒后才显示:

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

See pygame.display.set_icon() :pygame.display.set_icon()

[...] Some systems do not allow the window icon to change after it has been shown. [...] 某些系统不允许 window 图标在显示后更改。 This function can be called before pygame.display.set_mode() to create the icon before the display mode is set.在设置显示模式之前,可以在pygame.display.set_mode()之前调用此 function 来创建图标。

Set the icon before pygame.display.set_mode((1600, 900)) :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’) 

In addition, the size of the icon is limited:另外,图标的大小是有限制的:

[...] You can pass any surface, but most systems want a smaller image around 32x32. [...] 您可以通过任何表面,但大多数系统需要 32x32 左右的较小图像。

If the icon is not displayed, try a smaller icon.如果未显示图标,请尝试使用较小的图标。


Make sure that the resource (image) path and the working directory is correct.确保资源(图像)路径和工作目录正确。

The image file path has to be relative to the current working directory.图像文件路径必须相对于当前工作目录。 The working directory is possibly different to the directory of the python file.工作目录可能与 python 文件的目录不同。

The name and path of the file can be get by __file__ .文件的名称和路径可以通过__file__获取。 The current working directory can be get by os.getcwd() and can be changed by os.chdir(path) .当前工作目录可以通过os.getcwd()获取,也可以通过os.chdir(path)更改。

import os

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

An alternative solution is to find the absolute path.另一种解决方案是找到绝对路径。 If the image is relative to the folder of the python file (or even in the same folder), then you can get the directory of the file and join ( os.path.join() ) the image filename.如果图像相对于 python 文件的文件夹(甚至在同一文件夹中),则可以获取文件的目录并加入( os.path.join() )图像文件名。 eg:例如:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM