MJHanagan
Posts: 17
Joined: Tue Dec 27, 2016 6:37 pm

Auto start python script with tkinter content

Sun May 28, 2017 10:43 pm

I have my RPi 3 running the latest Raspbian version (Jessie 8) connected to a 7" touch screen and configured to boot into the Desktop GUI. I have a python script that uses the tkinter module to create a GUI for some home automation tasks (mainly turning various things on and off using a few remote XBee RF modules).

I want my python script (HomeV3.py) to automatically start after a reboot. I utilize crontab to run a few other python scripts at reboot and these all run as planned (but they do not utilize any tkinter GUI code). So I tried adding this latest script to run at reboot by adding this to the crontab:

Code: Select all

@reboot (/usr/bin/python3 /home/pi/MJH/HomeAuto/HomeV3.py & >> /home/pi/MJH/HomeAuto/Home.log)
But the script does not appear to run, however the Home.log file is created but is empty.

So I tried adding this to the /etc/rc.local file:

Code: Select all

sudo /usr/bin/python3 /home/pi/MJH/HomeAuto/HomeV3.py &
But the script does not run after a reboot.
When I type this from the CLI:

Code: Select all

sudo /etc/rc.local
the script runs as desired (so I assume it is coded in the rc.local file correctly)

Can anyone suggest what I should try next?

SurferTim
Posts: 1769
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Auto start python script with tkinter content

Mon May 29, 2017 3:13 am

I would try opening
./.config/lxsession/LXDE-pi/autostart
with a text editor and add your python script with a leading '@'
@python HomeV3.py
You might need a path for your script.

MJHanagan
Posts: 17
Joined: Tue Dec 27, 2016 6:37 pm

Re: Auto start python script with tkinter content

Mon May 29, 2017 1:55 pm

Brilliant! Thank you very much, it worked like a charm!

I added this line to the file:
@python /home/pi/MJH/HomeAuto/HomeV3.py

The "&" for running in background was not needed. The ">>" option for redirecting the script's "print" output did not work. Is there an option to open a CLI window in addition to the script's main GUI window?

SurferTim
Posts: 1769
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Auto start python script with tkinter content

Mon May 29, 2017 2:12 pm

I'm not certain what you mean, but you can try this in autostart
@lxterminal -e 'python /home/pi/MJH/HomeAuto/HomeV3.py'

MJHanagan
Posts: 17
Joined: Tue Dec 27, 2016 6:37 pm

Re: Auto start python script with tkinter content

Tue May 30, 2017 4:45 pm

That does open an lxterminal window but the HomeV3.py code did not launch.

What I really need to do is change my code to utilize "logging". I just discovered this python feature a few weeks ago and have been testing it out in another program. I has some nice advantages over using the normal "print" function. If I use logging rather than print I won't need another window to see what my code is currently up to. One of the nicest features of logging is being able to check the status remotely using my iPhone and an SSH connection.

You solved my main problem of getting the python script to launch automatically after a reboot. Thank you!!

I'm off to adjust my code.

vintozver
Posts: 27
Joined: Thu Jun 01, 2017 2:16 am
Location: Bellevue, WA, USA

Re: Auto start python script with tkinter content

Mon Jun 05, 2017 4:22 pm

Could you please be more specific in the "after reboot" term? GUI ready, after automatic login ...

You might find systemd events helpful (for example, after net up, after lxde up ...). Placing your stuff in /etc/rc.d or similar is not encouraged by the current Linux design.

I will be able to help when you clarify your requirements.

MJHanagan
Posts: 17
Joined: Tue Dec 27, 2016 6:37 pm

Re: Auto start python script with tkinter content

Tue Jun 06, 2017 8:03 pm

The code I wish to auto run used the tkinter module so the Raspberry Pi GUI needs to be up and running. The code also uses HTTP requests and needs to be sure the clock has synced to the internet time.

Return to “Advanced users”