首页 > 解决方案 > 将 PyGame 指向 Raspberry Pi OS Lite 上的帧缓冲区

问题描述

我正在使用 Pygame 编写一个 Python 应用程序,它将向连接(通过适配器)到我的Raspberry Pi Zero W的 HDMI 端口的 TFT 显示器输出接口。

这是我的代码的简化版本:

import os
import sys
import pygame
import time


if __name__ == "__main__":
    if sys.platform.startswith("linux"): # So I can develop and test on platforms other than Linux without this code running
        drivers = ['directfb', 'fbcon', 'svgalib']

        found = False
        for driver in drivers:
            if not os.getenv('SDL_VIDEODRIVER'):
                os.putenv('SDL_VIDEODRIVER', driver)
            try:
                pygame.display.init()
            except pygame.error:
                print('Driver: {0} failed.'.format(driver))
                continue
            found = True
            break
        if not found:
            raise Exception('No suitable video driver found!')
    else:
        pygame.display.init()

    screen = pygame.display.set_mode((pygame.display.Info().current_w, pygame.display.Info().current_h), pygame.FULLSCREEN)

    screen.fill((255, 255, 255))
    logo = pygame.image.load("./image.png").convert()

    screen.blit(logo, (0,0))

    pygame.display.update()

    time.sleep(5)

每次我使用python3. 以下是我在运行代码之前采取的步骤(我没有做任何其他事情):

  1. 从 Raspberry Pi 网站下载 Raspberry Pi OS。我正在使用Raspberry Pi OS Lite,发布日期为 2021 年 1 月 11 日,内核版本:5.4。
  2. 使用 BalenaEtcher 将图像闪存到微型 SD 卡。
  3. 插入 SD 卡并启动带有 TFT 显示屏(通过适配器)和无线 USB 键盘的 Raspberry Pi。
  4. 用于raspi-config启用串行控制台。
  5. 通过 PuTTY 和控制台 USB 电缆(波特率 115200,这是默认值)连接并登录到 Raspberry Pi。
  6. sudo apt update && sudo apt upgrade -y. 然后,我安装 Git 并生成一个 SSH 密钥,以便我可以使用部署密钥从 GitHub 获取我的代码。
  7. 运行python3(或sudo python3,这也会引发异常)。

我得到了我编程到我的代码中的异常:'No suitable video driver found!'

有人可以逐步解释一下,在第 7 步之后我需要做什么才能让 ./image.png 在 TFT 上显示 5 秒钟吗?此外,显然,我希望能够继续添加到我的程序中。

标签: pythonlinuxpygameraspberry-piframebuffer

解决方案


推荐阅读