0

I have a Raspberry Pi 4 Model B, and I try to start a very basic Tkinter GUI on reboot.

#!/home/pi/miniforge3/envs/myenv/bin/python

import tkinter as tk

def tk_gui():
    root = tk.Tk()
    root.geometry("200x200")
    label = tk.Label(root, text="Hello")
    label.pack(pady=20)
    root.mainloop()


if __name__ == "__main__":
    tk_gui()

I saved the above in a file called tk_basic.py and also changed the permissions chmod + x tk_basic.py. When I call it manually the GUI works.

I want to edit my Crontab so the GUI opens up on reboot.

@reboot XAUTHORITY=/home/pi/.Xauthority DISPLAY=:0 /home/pi/path/to/folder/tk_basic.py

But this doesn't work for me. My DISPLAY looks to be 0. since the command ps aux | grep "Xorg|PID" returns.

USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
pi          2047  0.0  0.0   6088  1920 pts/0    S+   13:01   0:00 grep -E --color=auto Xorg|PID

Even when I change cron to call my python executable like this:

@reboot XAUTHORITY=/home/pi/.Xauthority DISPLAY=:0 /home/pi/miniforge3/envs/myenv/bin/python /home/pi/path/to/folder/tk_basic.py

or even

 @reboot /home/pi/miniforge3/envs/myenv/bin/python /home/pi/path/to/folder/tk_basic.py

dont do any difference, the gui doesnt show up. I know there are similar posts like this or that but I couldnt make it work. Any ideas what I am missing?

To summarise, my crontab this is what my crontab looks like: (none of these works). I even tried DISPLAY=:1 to no avail. Other tasks in the crontab further below these lines are called succesfully.

@reboot XAUTHORITY=/home/pi/.Xauthority DISPLAY=:0 /home/pi/miniforge3/envs/myenv/bin/python /home/pi/path/to/folder/tk_basic.py
@reboot XAUTHORITY=/home/pi/.Xauthority DISPLAY=:0 /home/pi/path/to/folder/tk_basic.py
@reboot /home/pi/miniforge3/envs/myenv/bin/python /home/pi/dev/path/to/folder/tk_basic.py
@reboot /home/pi/path/to/folder/tk_basic.py
4
  • Are your sure the cron job is started after starting the X server?
    – acw1668
    Commented Jul 2 at 13:39
  • @acw1668, that’s a fair point actually. Do you know how to check this please?
    – Aenaon
    Commented Jul 2 at 15:04
  • Which one does the OS use: systemd or sysinit? If it is systemd, then you need to create a task that depends on task that starts the X server. I am not familiar about systemd, so you need to find it yourself. If it is sysinit, create a task in init level 5.
    – acw1668
    Commented Jul 2 at 15:05
  • tkinter apps need a display. Jobs started from cron typically do not have access to a display. See stackoverflow.com/questions/50801120/… Commented Jul 2 at 16:15

0

Browse other questions tagged or ask your own question.