用于Kivy上GUI的Raspberry Pi信息亭

哈Ha!



我想分享一下我的经验,即将Raspberry Pi 3B +设置为带有基于Python 3 Kivy库的GUI的信息亭。为什么选择Kivy?仅仅是我们已经有一个用Python开发的产品,我们想为其添加图形界面。值得注意的是,在Kivy之前,我们尝试了几种选择,包括wxWidgets甚至是带有Web应用程序的Chromium浏览器。事实证明,所有这些替代方法都无法抵抗Kivy,轻便和快速。Habré已经对该库进行了很好的概述



环境



我们将使用Raspbian Lite和Python 3.7以及Supervisor流程和服务管理系统。顺便说一句,Raspberry Pi Imager实用程序非常方便,您可以使用它准备SD卡。我们的宝宝RPI的第一次下载后,我们登录使用标准PI登录覆盆子密码然后我们执行:



$ sudo raspi-config


我们选择第五项“接口选项”,在出现的菜单中,我们对第二项SSH感兴趣,为了方便起见,我们将使用它启用远程访问。

因此,在您最喜欢的椅子上舒适地躺着后,我们将继续通过任何方便的ssh客户端配置RPi。



用户



让我们为用户创建一个方便的名称,让他使用sudo并重新启动:



$ sudo useradd -m kivygui -s /bin/bash
$ sudo passwd kivygui
$ sudo usermod -a -G sudo kivygui
$ sudo reboot


重新启动后,使用新数据通过ssh登录kivygui并删除标准pi帐户



$ sudo userdel pi
$ sudo rm -r /home/pi/


指出我们正在使用美式键盘布局并不是多余的:



$ sudo sed -i 's/XKBLAYOUT="gb"/XKBLAYOUT="us"/' /etc/default/keyboard


测试申请



我们的测试应用程序将位于单独的文件夹中,让我们创建它:



$ mkdir /home/kivygui/helloworld


现在,在nano文本编辑器中,让我们创建一个脚本来运行Python应用程序/home/kivygui/helloworld/run.sh,其内容如下:



export DISPLAY=:0.0
xset s off -dpms
exec matchbox-window-manager &
while true; do
  exec python3 start.py
done


让我们在文件/home/kivygui/helloworld/start.py中制作一个nano和一个简单界面的示例:



import kivy
kivy.require('1.11.0')
from kivy.app import App
from kivy.uix.label import Label
class MyApp(App):
    def build(self):
        return Label(text='Hello, world!')
if __name__ == '__main__':
    MyApp().run()


图形用户界面



我们需要pip3



$ sudo apt-get update -y
$ sudo apt-get install -y python3-pip


我还遇到了一个事实,即在安装nodm时,并非总是下载所有必需的软件包,因此,以防万一,我们将提前安装它们:



$ sudo apt-get install -y desktop-base gtk2-engines-pixbuf libxklavier16 xserver-xorg xserver-xorg-input-all xserver-xorg-input-libinput xserver-xorg-input-wacom xserver-xorg-legacy xserver-xorg-video-all xserver-xorg-video-amdgpu xserver-xorg-video-ati xserver-xorg-video-fbdev xserver-xorg-video-nouveau xserver-xorg-video-radeon xserver-xorg-video-vesa


现在让我们设置nodm和窗口管理器matchbox



$ sudo DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true apt-get install -y x11-xserver-utils nodm matchbox-window-manager
$ echo "/usr/sbin/nodm" | sudo tee /etc/X11/default-display-manager
$ sudo sed -i -e "s/NODM_ENABLED=false/NODM_ENABLED=true/" -e "s/NODM_USER=root/NODM_USER=kivygui/" -e "s/NODM_X_OPTIONS='-nolisten tcp'/NODM_X_OPTIONS='-nolisten tcp -nocursor'/" /etc/default/nodm


现在 轮到Kivy了



$ sudo apt-get install -y libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev pkg-config libgl1-mesa-dev libgles2-mesa-dev python-setuptools libgstreamer1.0-dev git-core gstreamer1.0-plugins-{bad,base,good,ugly} gstreamer1.0-{omx,alsa} python-dev libmtdev-dev xclip xsel libjpeg-dev
$ sudo python3 -m pip install --user kivy


现在,我们将教我们的系统向我们显示一个图形界面而不是一个控制台提示符,隐藏所有诊断消息并显示一个图形化系统引导屏幕:



$ sudo rm /etc/systemd/system/default.target
$ sudo rm /etc/systemd/system/getty@tty1.service.d/autologin.conf
$ sudo ln -s /lib/systemd/system/graphical.target /etc/systemd/system/default.target
$ sudo sed -i '$ s/$/ quiet splash consoleblank=0 loglevel=0 logo.nologo plymouth.ignore-serial-consoles/' /boot/cmdline.txt
$ sudo sed -i 's/console=tty1/console=tty3/' /boot/cmdline.txt


如果需要,可以完全禁用tty1控制台:



$ sudo systemctl disable getty@tty1


主管



现在让我们安装Supervisor



$ sudo apt-get install -y supervisor


让我们为日志创建一个文件夹:



$ mkdir /home/kivygui/logs


现在,让我们暂时停止Supervisor服务以重新配置:



$ sudo systemctl stop supervisor


使用nano编辑器将以下内容添加到配置文件/etc/supervisor/supervisord.conf中:



[program:rungui]
command=sh run.sh
directory=/home/kivygui/helloworld
user=root
autostart=true
autorestart=true
startsecs = 5
startretries=3
stderr_logfile=/home/kivygui/logs/rungui.err.log
stdout_logfile=/home/kivygui/logs/rungui.out.log
stderr_logfile_maxbytes=5MB
stdout_logfile_maxbytes=5MB
stopsignal=INT
stopwaitsecs=5


另外,让我们给kivyguiroot用户一些额外的选择。为此,请使用以下命令:



$ sudo visudo


让我们将文件转换为以下格式:



#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL
kivygui  ALL=(ALL:ALL) ALL
 
# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL
 
# See sudoers(5) for more information on "#include" directives:
 
#includedir /etc/sudoers.d
 
kivygui ALL = NOPASSWD: /usr/bin/supervisorctl
kivygui ALL = NOPASSWD: /usr/bin/python3.7
kivygui ALL=(ALL) NOPASSWD: /bin/systemctl daemon-reload
kivygui ALL=(ALL) NOPASSWD: /usr/bin/supervisord
 
root ALL=(ALL) NOPASSWD: /bin/systemctl daemon-reload
root ALL = NOPASSWD: /usr/bin/supervisorctl
root ALL = NOPASSWD: /usr/bin/python3.7
root ALL=(ALL) NOPASSWD: /usr/bin/supervisord


现在我们可以启动该服务:



$ sudo systemctl start supervisor


在连接到RPi的显示器上,我们将看到珍贵的问候。剩下的就是重新启动以测试图形启动屏幕。



聚苯乙烯



实际上,nodm可以autologin代替lightdm它将完全类似于nodm解决方案。此外,nodm开发人员本人也推荐这种方法。



All Articles