I want to start my GUI app wrote in python with tkinter at start my Rpi, I set the start method in the rc.local like this:
sudo bash -c 'python /home/pi/Desktop/appPython/good.py > /home/pi/Desktop/good.log 2>&1' &
But the app doesn't start and I get the next error in the log:
I
no display found
Traceback (most recent call last):
File "/home/pi/Desktop/appPython/good.py", line 10, in <module>
root=Tk()
File "/usr/lib/python3.7/tkinter/__init__.py", line 2023, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: couldn't connect to display ":0.0"
I select the display in my code with this method:
from tkinter import *
import sys
import os
if os.environ.get('DISPLAY', '')=='':
print('no display found')
os.environ.__setitem__('DISPLAY',':0.0')
I tried with DISPLAY=:0 and :0.0 with the same result. If I do
echo $DISPLAY
The result is
:0.0
Which can be the problem? How can I solve it?
rc.local(which is NOT a recommended way of starting programs on boot - search up Systemd) gets run before the desktop has "initialized." So tkinter can't make a window and therefore fails. For a quick, hacky solution, do aimport timeandtime.sleep(10)before the rest of your script. You may need to adjust the delay.