-1

我正在研究一个应该在 Raspberry Pi 启动期间打开的项目。我可以在启动时打开其他几个脚本,但是我在启动期间尝试打开的脚本没有打开。

注意:此代码在我通过 IDLE 运行时有效,但在我通过 LX 终端测试时无效。当我在自动运行脚本下测试脚本时,它给了我一个错误。
另一个注意事项:所有图像文件都与脚本位于同一文件夹中。

这是我的代码:

from Tkinter import *
from PIL import Image, ImageTk
from random import randint
import RPi.GPIO as GPIO

root = Tk()
root.overrideredirect(True)
root.geometry("1920x1080+0+0")

image=Image.open('bikebackground.png')
background_image = ImageTk.PhotoImage(image)
background_label = Label(root, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

speed = 4

class Biker(Label):
def __init__(self, master, filename):
    im = Image.open(filename)
    seq =  []
    try:
        while 1:
            seq.append(im.copy())
            im.seek(len(seq)) # skip to next frame
    except EOFError:
        pass # we're done

    try:
        self.delay = im.info['duration']/speed
    except KeyError:
        self.delay = 100

    first = seq[0].convert('RGBA')
    self.frames = [ImageTk.PhotoImage(first)]

    Label.__init__(self, master, image=self.frames[0])

    temp = seq[0]
    for image in seq[1:]:
        temp.paste(image)
        frame = temp.convert('RGBA')
        self.frames.append(ImageTk.PhotoImage(frame))

    self.idx = 0

    self.cancel = self.after(self.delay, self.play)

def play(self):
    self.config(image=self.frames[self.idx])
    self.idx += 1
    if self.idx == len(self.frames):
        self.idx = 0
    self.cancel = self.after(self.delay, self.play)

anim = Biker(root, 'racer2.gif')
anim.place(relx=0, rely=0, relwidth=1, relheight=1)

w = Label(root, text= 'You are generating\n%d.%d Watts\nof power' %(randint(10,15),
  randint(0,99)), font=("Helvetica", 50))
w.place(relx=.6, rely=.5, relwidth=.4, relheight=.25)

wh = Label(root, text= 'You have generated\n%d.%d Kilojoules\nof energy' %(randint(0,2),     
  randint(0,99)), font=("Helvetica", 50))
wh.place(relx=.6, rely=.75, relwidth=.4, relheight=.25)
root.mainloop()

在 LX 终端中,在这里测试代码时出现错误:

pi@raspberrypi ~/bin $ /home/pi/bin/script_auto_run
Doing autorun script...
pi@raspberrypi ~/bin $ Traceback (most recent call last):
 File "/home/pi/New/Display.py", line 10, in <module>
   image=Image.open('bikebackground.png')
  File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 2093, in open
   fp = builtins.open(fp, "rb")
IOError: [Errno 2] No such file or directory: 'bikebackground.png'
4

2 回答 2

1

似乎脚本不是从同一个文件夹运行的,所以它在工作目录中找不到图片。您应该将文件夹的绝对路径附加到图像路径:

import os.path

# Get the folder on the current file (/home/pi/New for your case)
SCRIPT_DIR = os.path.basename(__file__)

# and then use os.path.join to concatenate paths
image = Image.open(os.path.join(SCRIPT_DIR, 'bikebackground.png'))

.. 或在启动脚本之前明确移动到脚本的文件夹中。

于 2014-05-30T19:47:53.233 回答
0

我没有通过绝对路径,而是通过 LXDE 环境运行脚本,这使我可以轻松启动。这是通过打开 LX 终端并执行以下操作来完成的: sudo nano /etc/xdg/lxsession/LXDE/autostart

然后在自动启动文件中输入以下命令:@sudo python /home/pi/New/Display.py

然后重新启动RPi,它工作。

于 2014-06-01T03:08:47.453 回答