树莓派:如何在TFT显示器上显示Python输出

3
我正在尝试从Python脚本向Sainsmart 1.8 TFT显示屏输出一些数据。按照https://github.com/notro/fbtft/wiki的说明,我能够在TFT显示屏上显示树莓派的屏幕。
FRAMEBUFFER=/dev/fb1 startx

当我尝试从我的Python脚本输出数据时,一个pygame窗口(类似于“弹出”)会打开,但不会显示在连接到Raspberry的TFT屏幕上,而是显示在通过HDMI连接的主屏幕上。
我的代码来自这个教程
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# Creation:    26.05.2013
# Last Update: 07.04.2015
#
# Copyright (c) 2013-2015 by Georg Kainzbauer <http://www.gtkdb.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#

import os
import sys
import time
import pygame

time_stamp_prev=0

os.environ["SDL_FBDEV"] = "/dev/fb1"
os.environ['SDL_VIDEODRIVER']="fbcon"

def displaytext(text,size,line,color,clearscreen):
  if clearscreen:
    screen.fill((255,255,255))

  font = pygame.font.Font(None,size)
  text = font.render(text,0,color)
  rotated = pygame.transform.rotate(text,-90)
  textpos = rotated.get_rect()
  textpos.centery = 80
  if line == 1:
    textpos.centerx = 99
    screen.blit(rotated,textpos)
  elif line == 2:
    textpos.centerx = 61
    screen.blit(rotated,textpos)
  elif line == 3:
    textpos.centerx = 25
    screen.blit(rotated,textpos)

def main():
  global screen

  pygame.init()
  pygame.mouse.set_visible(0)
  size = width,height = 128,160
  screen = pygame.display.set_mode(size)

  while True:
    displaytext(time.strftime("%d.%m.%Y"),40,1,(0,0,0),True)
    displaytext(time.strftime("%H:%M:%S"),40,2,(0,0,0),False)
    displaytext("www.gtkdb.de",20,3,(0,0,0),False)
    pygame.display.flip()
    time.sleep(1)

if __name__ == '__main__':
  main()

有人可以告诉我如何在tft屏幕上显示脚本的输出吗?
1个回答

1
我看了一下startx命令的说明,觉得创建一个.xinitrc文件是解决方法。
尝试这样做:
chmod +x ./clock.py # make the clock file executable
mv ./clock.py ~/.xinitrc # move it to where startx starts it
FRAMEBUFFER=/dev/fb1 startx # startx will start it

注意:我在Lubuntu下测试过这个操作,仍然可以登录并使用用户界面。

非常感谢 - 它有效!你能否提供一个链接或者告诉我你是如何在此之后成功登录的?因为我的主屏幕变黑了,无法输入任何内容。 - user3255061
哦不,发帖太早了:(。我现在无法使用UI登录了。现在,在登录后键入“startx”后,它会在我的主屏幕上启动clock.py。 - user3255061
您可以使用Control+Alt+F3打开新的终端窗口,登录并使用nano ~/.xinitrc进行编辑。 - User

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接