0

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?

3
  • The problem is that 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 a import time and time.sleep(10) before the rest of your script. You may need to adjust the delay. Commented Apr 19, 2021 at 13:23
  • Thanks, but with the time.sleep(10) it doesn't work either, is there any possibility for made it work with rc.local? Commented Apr 19, 2021 at 14:29
  • Is the traceback still the same even with the time.sleep? Also can you edit your question to include the code with the sleep? (And the traceback) Commented Apr 19, 2021 at 17:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.