python - 使用 Raspberry Pi 启动时无法运行文件
问题描述
我已经尝试了多种方法(crontab 和 rc.local)来解决这个问题。我每次都对我的代码和流程进行了三次检查,但无济于事。
我正在用一个按钮制作父亲节礼物,上面写着他最喜欢的词“不!”。每次我重新启动时,我的程序根本不响应 GPIO 和附加的按钮。是我在我草率的代码中构建事物的方式吗?
我不是 Python 本地人,所以我请求你帮助我。
从终端运行或通过 Thonny 运行时,它运行良好。但无论如何,在启动时不会启动!
这是我的程序代码:
import pygame
from random import randint
import RPi.GPIO as GPIO
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
pygame.mixer.init()
#pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
continue
proverbFile = 'proverbs/Pro1.mp3'
num = 5
def randNo ():
noFile = '/home/pi/Desktop/audio/no/No1.mp3'
print('Executed!')
default = 1
global num
pulled = 1
while num == pulled:
pulled = randint(1,4)
print('Same!')
num = pulled
print(num)
noFile = noFile.replace(str(default), str(num), 1)
print(noFile)
pygame.mixer.music.load(noFile)
pygame.mixer.music.play()
default = num
def button_callback(channel):
print("Button was pushed!")
randNo()
GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback)
#while True:
# keyPress = input('Press q to quit: ')
# if keyPress == 'q':
# break;
#GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback) # Setup event on pin 10 rising edge
解决方案
如果您希望在启动到 LXDE 环境时运行脚本,您可以查看这个 Raspberry Pi 论坛帖子:
Navigate to ~/.config/lxsession/LXDE-pi
打开该文件夹中的自动启动文件:
$ sudo nano autostart
在新行上添加@midori。如果您想运行类似 python 脚本的内容,请将类似 @python mypython.py 的内容放在新行上。运行脚本文件将是@./superscript,但由于某种原因,脚本会在无限循环中运行(也许这会停止)。
Save and exit: Ctrl+X, Y, Enter
重新启动您的 Raspberry Pi 进入 LXDE 环境。
从这里回答
推荐阅读
- terraform - 您可以以及如何在安全/断开连接的环境中使用 Terraform 提供程序?
- javascript - IE11 对象不支持属性或方法“规范化”
- amazon-web-services - 将 AWS RDS 手动快照(postgres)移动/复制到 AWS s3 存储桶
- xamarin - 在异步方法之后初始化 xamarin 视图
- angularjs - 如何使用 AspNetCore 2.2 对 Angular 获取或发布请求进行身份验证和授权?
- html - 水平表单上的文本不垂直对齐
- docker - 无法 ping docker 主机,但世界是可达的
- java - 使用Java在不使用内置方法的情况下打印数组中的非重复整数元素
- .net - 带有 .NET Core 2.1 的 Angular 5 - 找不到模块,但可以
- r - 如何从 R 中的多个(数千个)netcdf 文件中写入单个 CSV 文件