简体   繁体   English

自动启动时 Tkinter GUI 无法正常运行

[英]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.这是我第一次使用 Raspberry Pi 3 和 Python Tkinter 来制作 GUI。

I have written my script using Python.我已经使用 Python 编写了我的脚本。 My code uses Raspberry Pi 3 GPIO 8 and GPIO 11 as inputs.我的代码使用 Raspberry Pi 3 GPIO 8 和 GPIO 11 作为输入。 I have 2 relay connected to the 2 inputs terminal.我有 2 个继电器连接到 2 个输入端子。 The relay will act as a switch.继电器将充当开关。 My code will count how many times the relay have switched on and display in the GUI.我的代码将计算继电器打开并显示在 GUI 中的次数。

I have tested it using Raspberry Pi 3 LxTerminal with我已经使用 Raspberry Pi 3 LxTerminal 和

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

The GUI opens and it works perfectly.图形用户界面打开,它完美地工作。 Every time the relay switches, the count +1.每次继电器切换时,计数+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 .我重新启动了我的 Raspberry Pi, GUI 会自动启动 But the GUI does not response to the relay switching.但 GUI响应继电器切换。

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.有时 GUI 工作,有时它不工作。

Anyone have come across this sorts of problem?有人遇到过这种问题吗? What should I do to make the GUI auto boot to works 100%?我应该怎么做才能使 GUI 自动启动 100% 工作?

NOTE:注意:

  • Having 5V 1A phone charger to supply power to Pi.有 5V 1A 手机充电器为 Pi 供电。 (Tried using 5V 2.1A as well) (也尝试使用 5V 2.1A)
  • Connected a HDMI to VGA converter to connect to a monitor连接 HDMI 转 VGA 转换器以连接到显示器
  • Does not have keyboard and mouse when the GUI is set to auto boot GUI 设置为自动启动时没有键盘和鼠标

I have a tkinter GUI that boots on startup on my Raspberry Pi.我有一个 tkinter GUI,可以在我的 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.作为 python 脚本的第一行。

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.现在您应该能够在没有“sudo python”前缀的情况下启动您的程序。 Just typing /home/pi/Desktop/test.py in the terminal should boot your program.只需在终端中输入/home/pi/Desktop/test.py启动您的程序。

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.顺便说一句:您使用“sudo”的方式太多了。 You should use sudo very rarely.您应该很少使用 sudo。 If you edit user files with sudo you can screw up your file permissions, which leads to very strange bugs.如果您使用 sudo 编辑用户文件,您可能会搞砸您的文件权限,这会导致非常奇怪的错误。 You should need "sudo" when you are installing something, and that's about it.当您安装某些东西时,您应该需要“sudo”,仅此而已。

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.首先是硬件推荐:抱歉引用了德语页面,但如下所示https://www.elektronik-kompendium.de/sites/raspberry-pi/1912111.htm你的 raspi 3 应该有 5v 和2A 但如果它支持高达 3A 就完美了。 But thats just some side info.但这只是一些侧面信息。

For me this question PyQt: How to run GUI on Raspberry Pi desktop startup?对我来说这个问题PyQt: How to run GUI on Raspberry Pi desktop startup? helped me solving the GUI autostart problem.帮助我解决了 GUI 自动启动问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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