0

私が取り組んでいる次のプログラムがあります (すべてを作成したわけではないため、一部はかなり奇妙です) 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 [file path]」を使用すると、「Traceback (most recent call last): File "/home/pi/Documents/alienPasscodeV2.py", line 10, in from omxplayer.player import OMXPlayer ImportError」と表示されます: 「omxplayer」という名前のモジュールはありません。

「python3 [ファイルパス]」だけを使用すると、ビデオを再生する必要があるまで動作し始めます。それからそれは私に言います:

「Tkinter コールバック トレースバックの例外 (最新の呼び出しが最後):
ファイル "/usr/lib/python3.5/tkinter/ init .py"、1562 行、呼び出しで return self.func(*args) ファイル "/home/pi /Documents/alienPasscodeV2.py"、86 行目、on_click7 順 (IM) ファイル "/home/pi/Documents/alienPasscodeV2.py"、121 行目、正しくないVideo() ファイル "/home/pi/Documents/alienPasscodeV2. py"、171 行目、incorrectVideo player = 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 行目、init内 self.load(source, pause=pause) File "/home/pi/.local/lib/python3.5/site-packages/omxplayer/player.py", line 245, in load self._load_source(source) File " /home/pi/.local/lib/python3.5/site-packages/omxplayer/player.py"、171 行目、_load_source self._connection = self._setup_dbus_connection(self._Connection, self._bus_address_finder) ファイル"/home /pi/.local/lib/python3.5/site-packages/omxplayer/player.py"、231 行目、_setup_dbus_connection で SystemError を発生させます ('DBus は OMXPlayer プロセスに接続できません') SystemError: DBus は OMXPlayer プロセスに接続できません"

sudo を使用する場合と使用しない場合に違いがある理由さえわかりません。私はこれを何時間も理解しようとしてきましたが、わかりません。こんな番組ですみません。

4

1 に答える 1