首页 > 解决方案 > 使用 Raspberry Pi 启动时无法运行文件

问题描述

我已经尝试了多种方法(crontab 和 rc.local)来解决这个问题。我每次都对我的代码和流程进行了三次检查,但无济于事。

我正在用一个按钮制作父亲节礼物,上面写着他最喜欢的词“不!”。每次我重新启动时,我的程序根本不响应 GPIO 和附加的按钮。是我在我草率的代码中构建事物的方式吗?

我不是 Python 本地人,所以我请求你帮助我。

从终端运行或通过 Thonny 运行时,它运行良好。但无论如何,在启动时不会启动!

这是我的 rc.local

这是我的程序代码:

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

标签: pythonpygame

解决方案


如果您希望在启动到 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 环境。

从这里回答


推荐阅读