module 'pygame' has no attribute 'display'
时间: 2023-09-05 21:12:49 浏览: 43
这个错误通常发生在使用pygame时,意味着你没有正确导入pygame.display模块。请确保在代码的开头添加以下导入语句:
```python
import pygame
from pygame import display
```
或者可以直接使用以下导入语句:
```python
from pygame import display
```
这样,你就能够正确使用`pygame.display`模块了。
相关问题
module 'pygame' has no attribute 'movie'
如果你运行上述代码遇到了 `module 'pygame' has no attribute 'movie'` 的错误,那么说明你使用的是 `pygame 2.x` 版本,而 `pygame.movie` 已经在 `pygame 2.x` 中被移除了。
为了解决这个问题,你可以使用 `pygame.freetype` 模块中的 `pygame.freetype.Font.render_to()` 方法来将视频帧渲染到 Pygame 窗口中。下面是一个使用 `pygame.freetype` 在 Python 中循环播放多个视频的示例代码:
```python
import pygame
import os
# 初始化 Pygame
pygame.init()
# 设置视频大小和窗口标题
size = (640, 360)
title = 'Video Player'
screen = pygame.display.set_mode(size)
pygame.display.set_caption(title)
# 设置播放列表和当前视频索引
playlist = ['test1.mp4', 'test2.mp4', 'test3.mp4']
index = 0
# 设置字体和字号
font = pygame.freetype.SysFont('Arial', 24)
# 循环播放视频
while True:
# 打开当前视频文件
file = playlist[index]
path = os.path.join(os.getcwd(), file)
video = pygame.movie.Movie(path)
video.set_display(screen, pygame.Rect((0, 0), size))
video.play()
# 播放视频
while video.get_busy():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
quit()
elif event.key == pygame.K_SPACE:
video.stop()
index = (index + 1) % len(playlist)
break
# 渲染视频帧
frame = video.get_surface()
screen.blit(frame, (0, 0))
# 显示当前视频名称
font.render_to(screen, (10, 10), file, (255, 255, 255))
# 更新窗口
pygame.display.flip()
video.stop()
```
这段代码使用 `pygame.freetype` 模块中的 `pygame.freetype.SysFont()` 方法创建了一个 Arial 字体对象,然后使用 `pygame.freetype.Font.render_to()` 方法在 Pygame 窗口中渲染视频帧和当前视频名称。
module 'pygame.display' has no attribute 'set'
根据引用中的代码和错误提示,可以看出在导入pygame模块后,尝试使用pygame.display.set_mode()函数时出现了错误,提示'module 'pygame' has no attribute 'display''。这个错误通常是由于pygame模块未正确安装或未正确导入所致。可以尝试重新安装pygame模块或者检查导入语句是否正确。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)