简体   繁体   中英

Tkinter GUI does not run properly when auto boot

this is my first time using Raspberry Pi 3 with Python Tkinter to make a GUI.

I have written my script using Python. My code uses Raspberry Pi 3 GPIO 8 and GPIO 11 as inputs. I have 2 relay connected to the 2 inputs terminal. The relay will act as a switch. My code will count how many times the relay have switched on and display in the GUI.

I have tested it using Raspberry Pi 3 LxTerminal with

sudo python /home/pi/Desktop/test.py

The GUI opens and it works perfectly. Every time the relay switches, the count +1.

Then I decided to make it auto boot every time I reboot my Pi. I uses the following technique.

sudo nano /etc/profile

Then add the following code to the final line and save it.

sudo python /home/pi/Desktop/test.py

I reboot my Raspberry Pi and the GUI boot automatically . But the GUI does not response to the relay switching.

I tried rebooting a few times and it worked after a few reboot . Then I tried to reboot again and the next time it does not work again . This steps keep repeating over and over again. Some times the GUI work, some times it doesn't.

Anyone have come across this sorts of problem? What should I do to make the GUI auto boot to works 100%?

NOTE:

  • Having 5V 1A phone charger to supply power to Pi. (Tried using 5V 2.1A as well)
  • Connected a HDMI to VGA converter to connect to a monitor
  • Does not have keyboard and mouse when the GUI is set to auto boot

I have a tkinter GUI that boots on startup on my Raspberry Pi. I did it by adding it to autostart file.

First, make the script executable. Add

#!/usr/bin/python

as the first line of your python script.

Then use this command to make the file executable

chmod +x /home/pi/Desktop/test.py

Now you should be able to start your program without the "sudo python" prefix. Just typing /home/pi/Desktop/test.py in the terminal should boot your program.

Then open autostart file for editing with

nano /home/pi/.config/lxsession/LXDE-pi/autostart

and add

@/home/pi/Desktop/test.py

BTW: you are using "sudo" way to much. You should use sudo very rarely. If you edit user files with sudo you can screw up your file permissions, which leads to very strange bugs. You should need "sudo" when you are installing something, and that's about it.

First of all a Hardware recommendation: sorry for quoting a german page but as shown here https://www.elektronik-kompendium.de/sites/raspberry-pi/1912111.htm your raspi 3 should have minimun a power supply from 5v and 2A but it would be perfect if it supports up to 3A. But thats just some side info.

For me this question PyQt: How to run GUI on Raspberry Pi desktop startup? helped me solving the GUI autostart problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM