Python raspberry pi触摸屏-Pygame.error:视频系统未初始化

Python raspberry pi触摸屏-Pygame.error:视频系统未初始化,python,pygame,touchscreen,raspberry-pi2,Python,Pygame,Touchscreen,Raspberry Pi2,我使用的是a中的一段代码,以下是我使用的部分: import pygame from pygame.locals import * import os from time import sleep import RPi.GPIO as GPIO #Setup the GPIOs as outputs - only 4 and 17 are available GPIO.setmode(GPIO.BCM) GPIO.setup(4, GPIO.OUT) GPIO.setup(17, GPIO.O

我使用的是a中的一段代码,以下是我使用的部分:

import pygame
from pygame.locals import *
import os
from time import sleep
import RPi.GPIO as GPIO

#Setup the GPIOs as outputs - only 4 and 17 are available
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)

#Colours
WHITE = (255,255,255)

os.putenv('SDL_FBDEV', '/dev/fb1')
os.putenv('SDL_MOUSEDRV', 'TSLIB')
os.putenv('SDL_MOUSEDEV', '/dev/input/touchscreen')

pygame.init()
pygame.mouse.set_visible(False)
lcd = pygame.display.set_mode((320, 240))
lcd.fill((0,0,0))
pygame.display.update()

font_big = pygame.font.Font(None, 50)

touch_buttons = {'17 on':(80,60), '4 on':(240,60), '17 off':(80,180), '4 off':(240,180)}

for k,v in touch_buttons.items():
    text_surface = font_big.render('%s'%k, True, WHITE)
    rect = text_surface.get_rect(center=v)
    lcd.blit(text_surface, rect)

pygame.display.update()

while True:
    # Scan touchscreen events
    for event in pygame.event.get():
        if(event.type is MOUSEBUTTONDOWN):
            pos = pygame.mouse.get_pos()
            print pos
        elif(event.type is MOUSEBUTTONUP):
            pos = pygame.mouse.get_pos()
            print pos
            #Find which quarter of the screen we're in
            x,y = pos
            if y < 120:
                if x < 160:
                    GPIO.output(17, False)
                else:
                    GPIO.output(4, False)
            else:
                if x < 160:
                    GPIO.output(17, True)
                else:
                    GPIO.output(4, True)
    sleep(0.1)
导入pygame
从pygame.locals导入*
导入操作系统
从时间上导入睡眠
将RPi.GPIO导入为GPIO
#将GPIO设置为输出-只有4和17可用
GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.OUT)
GPIO.setup(17,GPIO.OUT)
#颜色
白色=(255255)
os.putenv('SDL_FBDEV','/dev/fb1')
os.putenv('SDL_MOUSEDRV','TSLIB')
os.putenv('SDL_MOUSEDEV','/dev/input/touchscreen'))
pygame.init()
pygame.mouse.set_可见(False)
lcd=pygame.display.set_模式((320240))
液晶显示填充((0,0,0))
pygame.display.update()
font\u big=pygame.font.font(无,50)
触摸按钮={'17 on':(80,60),'4 on':(240,60),'17 off':(80180),'4 off':(240180)}
对于k、v触控按钮。项()
text\u surface=font\u big.render(“%s”%k,真,白色)
rect=文本\曲面。get\ rect(中心=v)
lcd.blit(文本表面,矩形)
pygame.display.update()
尽管如此:
#扫描触摸屏事件
对于pygame.event.get()中的事件:
如果(event.type为MOUSEBUTTONDOWN):
pos=pygame.mouse.get_pos()
打印位置
elif(event.type为MOUSEBUTTONUP):
pos=pygame.mouse.get_pos()
打印位置
#找到我们在屏幕的哪个区域
x、 y=位置
如果y<120:
如果x<160:
GPIO.output(17,False)
其他:
GPIO.output(4,False)
其他:
如果x<160:
GPIO.output(17,真)
其他:
GPIO.output(4,真)
睡眠(0.1)
当我(以root权限)运行它时,会出现以下错误:

  File "touchscreen.py", line 20, in <module>
    pygame.mouse.set_visible(False)
pygame.error: video system not initialized
文件“touchscreen.py”,第20行,在
pygame.mouse.set_可见(False)
pygame.error:视频系统未初始化
我正在尝试为我的项目在raspberry pi触摸屏上创建一个简单的按钮界面,任何可行的解决方案都会受到欢迎。这对我没有关系,如果这个代码是修复替代代码将同样有效

谢谢,


Matthew Wood

我已经解决了这个问题,解决方案是输入设备被称为mouse0而不是触摸屏。

试试这个:

disp_no = os.getenv('DISPLAY')
if disp_no:
    print "I'm running under X display = {0}".format(disp_no)
    pygame.mouse.set_visible(True)

else:
    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
        pygame.mouse.set_visible(False)
        break

    if not found:
        raise Exception('No suitable video driver found!')

os.environ["SDL_FBDEV"] = "/dev/fb1"
os.environ["SDL_MOUSEDEV"] = "/dev/input/touchscreen"
os.environ["SDL_MOUSEDRV"] = "TSLIB"
例如,就在k、v线的正上方

这将尝试在X-Windows(使用可见的鼠标指针)中运行,或者在最佳可用的帧缓冲区驱动程序中运行