我在 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 [文件路径]”,它会告诉我:“Traceback(最近一次调用最后一次):文件“/home/pi/Documents/alienPasscodeV2.py”,第 10 行,从 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(source, pause=pause) 文件“/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”,第 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 raise SystemError('DBus cannot connect to the OMXPlayer process') SystemError: DBus cannot connect to the OMXPlayer process "
我不知道为什么使用 sudo 和不使用 sudo 之间甚至有区别。我一直试图弄清楚这个几个小时,我不知道。我很抱歉这个节目。