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
systemd
orsysinit
? If it issystemd
, then you need to create a task that depends on task that starts the X server. I am not familiar aboutsystemd
, so you need to find it yourself. If it issysinit
, create a task in init level 5.