从终端运行时的Omxplayer-wrapper问题

时间:2018-11-10 17:19:07

标签: python python-3.x tkinter raspberry-pi omxplayer

我在Raspberry Pi上正在开发以下程序(我并未创建所有程序,因此其中有些很奇怪)。基本上应该打开一个带有按钮的tkinter窗口,如果按正确的顺序单击按钮,将开始一个新过程。如果按错误的顺序单击按钮,它将播放视频。 所有这些操作都可以在Thonny IDE中使用。(实际上,程序的细节并不重要,除了它使用omxplayer-wrapper播放视频外)

import os, subprocess
import RPi.GPIO as GPIO
import sys
import os
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
import time
from omxplayer.player import OMXPlayer
from pathlib import Path

GPIO.setmode(GPIO.BCM)


# Variables
wrongOrder=False
desired_order=[1,2,3,8,9,10] #Desired order of clicking on images
click_num=0  #Number of clicks
videoPath = "MOV5.mp4"
loopPath = "loop2.mp4"
incorrectPath = "Incorrect Password.mp4"
secondVideoPath = "Access Denied.mp4"
secondLoopPath = "loop1.mp4"
GPIO.setup(20, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(19, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(18, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(2, GPIO.IN) #The input for when the power rod is removed (stop 1st looping video)
GPIO.setup(3, GPIO.IN) #The input for when to stop the 2nd looping video




# Actions taken when clicked on each image
def on_click1(event=None):
    global IM
    print("image1 clicked")
    IM=1 #the image number
    order(IM)


def on_click2(event=None):
    global IM
    print("image2 clicked")
    IM=2
    order(IM)


def on_click3(event=None):
    global IM
    print("image3 clicked")
    IM=3
    order(IM)


def on_click4(event=None):
    global IM
    print("image4 clicked")
    IM=4
    order(IM)

def on_click5(event=None):
    global IM
    print("image5 clicked")
    IM=5
    order(IM)

def on_click6(event=None):
    global IM
    print("image6 clicked")
    IM=6
    order(IM)

def on_click7(event=None):
    global IM
    print("image7 clicked")
    IM=7
    order(IM)

def on_click8(event=None):
    global IM
    print("image8 clicked")
    IM=8
    order(IM)

def on_click9(event=None):
    global IM
    print("image9 clicked")
    IM=9
    order(IM)

def on_click10(event=None):
    global IM
    print("image10 clicked")
    IM=10
    order(IM)

root = tk.Tk()

#Function when an image is clicked
def order(IM):
    global click_num, wrongOrder
    if (IM != desired_order[click_num]): #If this is not the right button
        wrongOrder = True 
    click_num += 1
    aCanvas.itemconfigure(asterisks, text = aCanvas.itemcget(asterisks, "text")+"*") #Adds an asterisk to the text
    if (click_num == 6): #If it is the last number
        if (wrongOrder): 
            print("Wrong Order!")
            click_num = 0
            wrongOrder = False
            aCanvas.itemconfigure(asterisks, text = "") #Set text back to no asterisks
            incorrectVideo()
        else:
            print("Correct Order!")
            afterPassword()


def afterPassword():
    playVideo()
    time.sleep(11)  #Wait before door opens
    PIN()       #play_video, pin low
    time.sleep(8)
    root.destroy()
    playFirstLoop() #This will have to wait for a GPIO pin to change
    playSecondVideo()
    PIN2()#Trigger different relay
    relayFlash()
    piOutput() #Trigger other pi 
    playSecondLoop()




def PIN():
    GPIO.setup(21, GPIO.OUT, initial=GPIO.LOW)           # set GPIO21 as an output
    #GPIO.output(21, 0)         # set GPIO21 to 0/GPIO.LOW/True
def PIN2():
    GPIO.output(20,0)
def playVideo():
    player = OMXPlayer(videoPath, args=["--win", "0 0 1280 1024"])

def playFirstLoop(): 
    player = OMXPlayer(loopPath, args=["--loop","--no-osd", "--win", "0 0 1280 1024"])
    while(True):
        if (GPIO.input(2)==False): 
            player.quit()
            break

def playSecondVideo():
    player = OMXPlayer(secondVideoPath, args=["--win", "0 0 1280 1024"])
    player.quit()


def playSecondLoop(): 
    player = OMXPlayer(secondLoopPath, args=["--loop","--no-osd", "--win", "0 0 1280 1024"])
    while(True):
        if (GPIO.input(3)==False): 
            player.quit()
            break

def incorrectVideo():
    player = OMXPlayer(incorrectPath, args=["--win", "0 0 1280 1024"], dbus_name='org.mpris.MediaPlayer2.omxplayer0')
    time.sleep(2)
    player.quit()

def relayFlash():
    GPIO.output(19,0)
    time.sleep(0.5)
    for i in range(0,14):
        GPIO.output(19,1)
        time.sleep(0.5)
        GPIO.output(19,0)
        time.sleep(0.5)
    GPIO.output(19,1)

def piOutput():
    GPIO.output(18,1)


size_x=249
size_y = 350
#Screen resolution: 1245 x 1044

# load images
image1 = Image.open("/home/pi/Documents/1.jpg")
image1 = image1.resize((size_x, size_y), Image.ANTIALIAS)
photo1 = ImageTk.PhotoImage(image1)

image2 = Image.open("/home/pi/Documents/2.jpg")
image2 = image2.resize((size_x, size_y), Image.ANTIALIAS)
photo2 = ImageTk.PhotoImage(image2)

image3 = Image.open("/home/pi/Documents/3.jpg")
image3 = image3.resize((size_x, size_y), Image.ANTIALIAS)
photo3 = ImageTk.PhotoImage(image3)

image4 = Image.open("/home/pi/Documents/4.jpg")
image4 = image4.resize((size_x, size_y), Image.ANTIALIAS)
photo4 = ImageTk.PhotoImage(image4)

image5 = Image.open("/home/pi/Documents/5.jpg")
image5 = image5.resize((size_x, size_y), Image.ANTIALIAS)
photo5= ImageTk.PhotoImage(image5)

image6 = Image.open("/home/pi/Documents/6.jpg")
image6 = image6.resize((size_x, size_y), Image.ANTIALIAS)
photo6 = ImageTk.PhotoImage(image6)

image7 = Image.open("/home/pi/Documents/7.jpg")
image7 = image7.resize((size_x, size_y), Image.ANTIALIAS)
photo7 = ImageTk.PhotoImage(image7)

image8 = Image.open("/home/pi/Documents/8.jpg")
image8 = image8.resize((size_x, size_y), Image.ANTIALIAS)
photo8 = ImageTk.PhotoImage(image8)

image9 = Image.open("/home/pi/Documents/9.jpg")
image9 = image9.resize((size_x, size_y), Image.ANTIALIAS)
photo9 = ImageTk.PhotoImage(image9)

image10 = Image.open("/home/pi/Documents/0.jpg")
image10 = image10.resize((size_x, size_y), Image.ANTIALIAS)
photo10 = ImageTk.PhotoImage(image10)


buttonFrame = Frame(root)
# button with image binded to the same function
b1 = tk.Button(buttonFrame, image=photo1, command=on_click1)
b2 = tk.Button(buttonFrame, image=photo2, command=on_click2)
b3 = tk.Button(buttonFrame, image=photo3, command=on_click3)
b4 = tk.Button(buttonFrame, image=photo4, command=on_click4)
b5 = tk.Button(buttonFrame, image=photo5, command=on_click5)
b6 = tk.Button(buttonFrame, image=photo6, command=on_click6)
b7 = tk.Button(buttonFrame, image=photo7, command=on_click7)
b8 = tk.Button(buttonFrame, image=photo8, command=on_click8)
b9 = tk.Button(buttonFrame, image=photo9, command=on_click9)
b10 = tk.Button(buttonFrame, image=photo10, command=on_click10)

aFrame = Frame(root,bg="white") #Make a new frame containing the canvas
aCanvas = Canvas(aFrame, width = 747, height = 324) #Make canvas for asterisks
aCanvas.create_rectangle(0,0,747,324, fill="white", width = 15) #Make rectangle for asterisks to display on
squarePaddingX = 0
squarePaddingXStart = 25
squarePaddingY = 87
squareLength = 117
for i in range(0,6):
    aCanvas.create_rectangle((squarePaddingX * (i) + squarePaddingXStart)+(squareLength * i),squarePaddingY,(squarePaddingX*(i))+(squareLength * (i+1) + squarePaddingXStart),squareLength+squarePaddingY, width = 5) #Crazy code that creates squares. Set variables above, don't touch this
asterisks = aCanvas.create_text(25,50,text="",font=("Verdana", "175"), anchor=tk.NW) #Make text for asterisks
aCanvas.pack()



b1.grid(row=0,column=0)
b2.grid(row=0,column=1)
b3.grid(row=0,column=2)
b4.grid(row=0,column=3)
b5.grid(row=0,column=4)
b6.grid(row=1,column=0)
b7.grid(row=1,column=1)
b8.grid(row=1,column=2)
b9.grid(row=1,column=3)
b10.grid(row=1,column=4)
buttonFrame.grid(row=0) #Set buttonframe above asterisk frame
aFrame.grid(row=1)

root.mainloop()

GPIO.cleanup()                 #resets all GPIO ports used by this program

我的问题是我希望它在Pi启动时运行,这需要从终端启动程序。如果我使用“ sudo python3 [文件路径]”,它会告诉我:“跟踪(最近一次调用是最后一次):   在第10行中输入文件“ /home/pi/Documents/alienPasscodeV2.py”     从omxplayer.player导入OMXPlayer ImportError:没有名为“ omxplayer”的模块。

如果我仅使用“ python3 [文件路径]”,它将开始工作,直到需要播放视频为止。然后它告诉我:

  

” Tkinter回调Traceback中的异常(最近一次调用最后一次):
  在调用中,文件“ /usr/lib/python3.5/tkinter/init.py”,第1562行       返回self.func(* args)文件“ /home/pi/Documents/alienPasscodeV2.py”,行86,位于on_click7中       按顺序订购(IM)文件“ /home/pi/Documents/alienPasscodeV2.py”,第121行       不正确的视频()文件“ /home/pi/Documents/alienPasscodeV2.py”,第171行,不正确的视频       播放器= OMXPlayer(incorrectPath,args = [“-win”,“ 0 0 1280 1024”],dbus_name ='org.mpris.MediaPlayer2.omxplayer0')文件   “ /home/pi/.local/lib/python3.5/site-packages/omxplayer/player.py”,   第162行,初始化       self.load(源,暂停=暂停)文件“ /home/pi/.local/lib/python3.5/site-packages/omxplayer/player.py”,   245行,在负载中       self._load_source(source)文件“ /home/pi/.local/lib/python3.5/site-packages/omxplayer/player.py”,   _load_source中的第171行       self._connection = self._setup_dbus_connection(self._Connection,self._bus_address_finder)文件   “ /home/pi/.local/lib/python3.5/site-packages/omxplayer/player.py”,   _setup_dbus_connection中的第231行       引发SystemError(“ DBus无法连接到OMXPlayer进程”)SystemError:DBus无法连接到OMXPlayer进程”

我不知道为什么在使用sudo和不使用sudo之间甚至会有区别。我已经尝试了好几个小时了,但我不知道。我对此程序感到很抱歉。

1 个答案:

答案 0 :(得分:0)

结果是,当我设置路径时,我需要设置完整路径,而不仅仅是文件名。因此,路径更改为/ home / pi / Documents / filename。