使用触摸屏启动时运行 Python 脚本时显示名称错误 [英] Bad Display Name when running Python script on boot with Touch Screen

查看:99
本文介绍了使用触摸屏启动时运行 Python 脚本时显示名称错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在 Raspberry Pi 3B+ 1GB RAM、Raspbian 上启动时运行 Python 脚本,带有 SunFounder 10" 触摸屏, - .log 文件返回 "Bad display name'

Attempting to run a Python script on boot on Raspberry Pi 3B+ 1GB RAM, Raspbian, with a SunFounder 10" Touch Screen, - .log file returns "Bad display name'

Python 脚本在通过终端/可执行脚本/Thonny 等运行时 100% 运行.尝试在启动时首先通过 rc.local 运行 - 创建了一个服务,启用了服务,守护进程重新加载...等.没有工作.尝试作为 crontab 运行,结果相同 - crontab 的 .log 输出显示显示名称错误".认为缺少在 Python 脚本中导入和声明的显示环境,所以我添加了它 - 但在启动时返回相同的结果.

Python script is 100% functional when run via Terminal / executable script / Thonny etc. Attempted to run at boot first via rc.local - created a service, enabled service, daemon-reload... etc. Did not work. Tried to run as crontab, same result - .log output from crontab shows "Bad display name". Thought it was lack of Display Environment imported and declared within the Python script, so I added that - but on boot returns the same result.

这是我正在使用的 Python 脚本

This is the Python Script I'm using

#!/usr/bin/env python3

import RPi.GPIO as GPIO
import os
import sys
import webbrowser
import time
import subprocess
from pynput import keyboard
from Xlib.display import Display

#GPIO Readout
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

#GPIO Header Setup
header = 2
GPIO.setup(header, GPIO.IN)

#Omxplayer Commands
Loop = 'omxplayer -b --loop --no-osd -o hdmi /home/pi/Videos/PlanetEarth.mp4 > /dev/null'
Donation = 'omxplayer -b --no-osd -o hdmi /home/pi/Videos/Cartoon.mp4 > /dev/null'
KillPlayer = 'pkill omxplayer.bin'
KillForm = 'pkill chromium'

#Set Display Environment
new_env = dict(os.environ)
new_env['DISPLAY'] = ':0.0'

#Form Handling Required Below
#If Donation is successful, Stop Looping Video, Open Form in Chromium, Wait 60 seconds, Close Chromium, Restart Loop
def PullDownSuccess():
    subprocess.Popen(KillPlayer, env=new_env, shell=True)
    time.sleep(2)
    webbrowser.open('<url>')
    time.sleep(60)
    subprocess.Popen(KillForm, env=new_env, shell=True)
    time.sleep(2)
    subprocess.Popen(Loop, env=new_env, shell=True)

#Inception
subprocess.Popen(Loop, env=new_env, shell=True)

#Terminate Loop with Escape Key or Manually Initiate Donation Success
def on_press(key):
    if key == keyboard.Key.ctrl:
        PullDownSuccess()
    if key == keyboard.Key.esc:
        subprocess.Popen(KillPlayer, shell=True)

#Keyboard Listener Module
with keyboard.Listener(
        on_press=on_press) as listener:
    listener.join()

#Donation Successful Do:
while True:
    header_state = GPIO.input(header)
    if header_state == GPIO.HIGH:
        PullDownSuccess()

我目前正在尝试通过 crontab 使用以下行在 Boot 上运行此脚本:

I am currently attempting to run this script on Boot via crontab with this line:

@reboot (/bin/sleep 10; /usr/bin/python3 /home/pi/Custom_Scripts/<script>.py > /home/pi/Custom_Scripts/cronjoblog 2>&1)

crontab 的错误日志文件返回以下内容:

The error log file for crontab returns the following:

    raise error.DisplayNameError(display)
Xlib.error.DisplayNameError: Bad display name ""

此错误仅在尝试在启动时运行脚本时存在.显示器是否覆盖了启动时的启动显示权限?是什么阻止脚本在启动时在显示器上运行,但在远程执行时不运行?感谢您的考虑.

This error only exists when attempting to run script on boot up. Is the Display overriding the boot display permissions on Boot Up? What is keeping the script from running on the Display on boot up, but not when remotely executed? Thank you for your consideration.

更新:仍然没有解决方案.显示环境返回:0.0'……到目前为止我已经尝试删除

Update: Still no solution. Display environment returns ":0.0' ... so far I have tried to remove

> /dev/null from #Omxplayer Commands

将 crontab 启动行替换为:

Replacing crontab startup line to:

DISPLAY=":0" /usr/bin/python3 /home/pi/Custom_Scripts/<script>.py

DISPLAY=":0.0" /usr/bin/python3 /home/pi/Custom_Scripts/<script>.py

以及这些的任何可能组合.

And any possible combination of these.

确认脚本没有等待任何后台进程,因为我添加了最多 30 秒的延迟 (time.sleep),以及返回的 IP 地址等.

Confirmed the script is not waiting for any background processes as I have added delay (time.sleep) up to 30 seconds, as well as returning IP addresses, etc.

仍然返回错误的显示名称或无法连接到显示:0":b'无效的 MIT-MAGIC-COOKIE-1 密钥"

Returns either Bad Display Name still OR "Can't connect to display ":0": b'Invalid MIT-MAGIC-COOKIE-1 key"

如果有人有解决方案,仍在寻找解决方案.

Still looking for a solution if anyone has one.

使用/LXDE-pi/autostart 修复.在下面回答.

Fixed using /LXDE-pi/autostart. Answer below.

推荐答案

如果使用此方法有不可预见的后果,我会相应地更新此线程.我只是通过在/etc/sdg/lxsession/LXDE-pi/autostart 中的自动启动文件中添加两行来解决这个问题.

If there are unforeseen ramifications for using this method, I will update this thread accordingly. I fixed this issue just by adding two lines to the autostart file which resides in /etc/sdg/lxsession/LXDE-pi/autostart.

sudo nano /etc/xgd/lxsession/LXDE-pi/autostart

添加这两行,不要修改现有代码.

Add these two lines, do not modify the existing code.

sleep 5
@/usr/bin/python3 /home/pi/Custom_Scripts/<script>.py

我的自动启动文件如下所示:

My autostart file looks like this:

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
point-rpi
sleep 5
@usr/bin/python3 /home/pi/Custom_Scripts/<script>/py

Ctrl + O 写入文件

Ctrl + O to write file

sudo reboot

这篇关于使用触摸屏启动时运行 Python 脚本时显示名称错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆