无法在启动时运行 python 脚本。树莓派4

无法在启动时运行 python 脚本。树莓派4

我试图在 Raspberry pi 4 启动时运行 python 脚本。当我直接运行这个Python脚本时,它就可以工作,没有错误。我已经编辑了 crontab 文件来执行此操作,如下所示:

# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
@reboot python /home/pi/FUZEGUI.py > /home/pi/FUZEGUIlog.txt

日志文件中没有任何信息。我尝试了一种不同的方法,rc.local

# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi
printf "Launching FUZE GUI"
sudo python home/pi/FUZEGUI.py &
exit 0

但这也行不通。 脚本

#!/usr/bin/env python
try:
    import Tkinter as tk
except:
    import tkinter as tk
from PIL import Image, ImageTk
import time
import threading

root = tk.Tk()
root.attributes('-fullscreen', True)
root.geometry("800x800")

# Define Canvas
canvas = tk.Canvas(root, width=800, height=800)
canvas.grid(row=0, column=0)


# translates an rgb tuple of int to a tkinter friendly color code
def _from_rgb(rgb):
    return "#%02x%02x%02x" % rgb


# Called when user presses View Log button
def viewLogRaise():
    # Hide Previous Windows
    canvas.itemconfigure(logButtonWindow, state="hidden")
    canvas.itemconfigure(titleLabelWindow, state="hidden")
    # Open Closed Windows
    canvas.itemconfigure(backButtonWindow, state="normal")
    canvas.itemconfigure(logTextWindow, state="normal")
    canvas.itemconfigure(scrollbarWindow, state="normal")
    #I want to read the file continuously at this point.
    t =threading.Thread(target=readFile)
    t.start()

def backToMenu():
    # Hide Previous Windows
    canvas.itemconfigure(backButtonWindow, state="hidden")
    canvas.itemconfigure(logTextWindow, state="hidden")
    canvas.itemconfigure(scrollbarWindow, state="hidden")
    # Open Closed Windows
    canvas.itemconfigure(logButtonWindow, state="normal")
    canvas.itemconfigure(titleLabelWindow, state="normal")


def follow(thefile,reading):
    count= 0
    thefile.seek(0, 2)
    count += 1
    while reading == True:
        line = thefile.readline()
        if not line:
            time.sleep(0.1)
            continue
        yield line
def readFile():
    logfile = open("fast.log", "r")
    loglines = follow(logfile,True)
    for line in loglines:
        logText.insert(tk.END, line)
    


# Background
pathToGif = "redpoly4.jpg"
# red_background=Image.open("redBackground.gif")
backgroundImage = ImageTk.PhotoImage(file=pathToGif)
canvas.background = backgroundImage
bg = canvas.create_image(0, 0, anchor=tk.NW, image=backgroundImage)
titleLabel = tk.Label(root, fg="white", text="FUZE", borderwidth=2, relief="solid", bg=_from_rgb((239, 36, 37)),
                      font=("Courier", 100))
titleLabelWindow = canvas.create_window(400, 120, window=titleLabel)
logButton = tk.Button(root, fg="white", text="View Log", command=viewLogRaise, borderwidth=2, relief="raised",
                      bg=_from_rgb((239, 36, 37)), font=("Courier", 46))
logButtonWindow = canvas.create_window(400, 320, window=logButton)
backButton = tk.Button(root, fg="white", text="Back", command=backToMenu, borderwidth=2, relief="raised",
                       bg=_from_rgb((239, 36, 37)), font=("Courier", 20))
backButtonWindow = canvas.create_window(70, 360, window=backButton)



canvas.itemconfigure(backButtonWindow, state="hidden")
logText = tk.Text(root, bg="white", height=22, width=60, borderwidth=2, relief="solid")
logTextWindow = canvas.create_window(450, 208, window=logText)
# attach text widget to scrollbar
scrollbar = tk.Scrollbar(root)
scrollbar.grid(row=1,column=3)
scrollbarWindow = canvas.create_window(710,200, window=scrollbar)
logText.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=logText.yview)
canvas.itemconfigure(scrollbarWindow,state="hidden")
canvas.itemconfigure(logTextWindow, state="hidden")
root.mainloop()

相关内容