无法更新更改 GPIO 引脚的图像
Can't update images changing GPIO pins
我正在尝试创建一个程序,它使用 BCM 设置模式简单地读取 2 个引脚,在本例中为引脚 17 和引脚 27。
根据每个引脚的值(0 或 1),程序将在屏幕上显示不同的图像。
我的问题是,我第一次 运行 程序显示的图像是正确的,但如果同时我将任何引脚设置为不同的状态 (ON/OFF),程序不会' 在屏幕上上传正确的图像,显示错误消息。
如果我只是尝试 运行 "prints",它工作正常,但对于图像我总是有这个问题。我从不在第一张图片之后就通过。整个程序都停留在初始图像上。我在 create_image return self._create.
中收到错误
编辑:下面 link 中的错误消息图片
error i get https://i.imgur.com/50BxkFp.png
import Tkinter as tk
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.IN)
GPIO.setup(27,GPIO.IN)
app = tk.Tk()
app.attributes("-fullscreen", True)
app.title('Presence State')
screen_width = app.winfo_screenwidth()
screen_height = app.winfo_screenheight()
emptyP = tk.PhotoImage(file = "./images/emptyPresence.jpg")
leftP = tk.PhotoImage(file = "./images/leftPresence.jpg")
rightP = tk.PhotoImage(file = "./images/rightPresence.jpg")
bothP = tk.PhotoImage(file = "./images/bothSidesPresence.jpg")
fname = tk.Canvas(app, bg = "black" , width = screen_width, height =
screen_height)
def empty():
image = fname.create_image(screen_width/2, screen_height/2, anchor =
tk.CENTER, image = emptyP)
def left():
image = fname.create_image(screen_width/2, screen_height/2, anchor =
tk.CENTER, image = leftP)
def right():
image = fname.create_image(screen_width/2, screen_height/2, anchor =
tk.CENTER, image = rightP)
def both():
image = fname.create_image(screen_width/2, screen_height/2, anchor =
tk.CENTER, image = bothP)
while(1):
if GPIO.input(17) == 0 and GPIO.input(27) == 0:
empty()
time.sleep(.5)
elif GPIO.input(17) == 1 and GPIO.input(27) == 0:
left()
time.sleep(.5)
elif GPIO.input(17) == 0 and GPIO.input(27) == 1:
right()
time.sleep(.5)
else:
both()
time.sleep(.5)
fname.pack()
app.mainloop()
我没有 RaspberryPi,所以我创建了两个 Checkbutton 来模拟 IO 引脚。我已经在标签中显示了结果。修改结果以显示图像应该不会太困难。函数 on_after 每 500 毫秒调用一次自身以代替原来的 sleep(0.5)s
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('400x100')
root.title('"After" Demonstation')
# Set up fake GPIO Pins as Checkbuttons
# IntVars to easily read the state.
gpio17 = tk.IntVar()
gpio27 = tk.IntVar()
# Initialise to zero
gpio17.set(0)
gpio27.set(0)
ttk.Checkbutton(root, variable = gpio17, text = 'GPIO 17', width = 20).grid( row = 0, column = 0)
ttk.Checkbutton(root, variable = gpio27, text = 'GPIO 27', width = 20).grid( row = 0, column = 2)
# I'm using a label for simplicity. Amendments required
# below to cope with images.
result = tk.StringVar()
ttk.Label(root, textvariable = result, width = 20 ).grid(row=1, column=1)
DELAY = 500 # in milliseconds
def on_after():
""" Reads the state of the io pins and sets the result
to the appropriate value. """
# Code to fetch the IO states will be different with real pins.
io17 = gpio17.get() # Get the button states
io27 = gpio27.get()
# Set the result. Will be different with images.
if io17:
if io27:
result.set('Both')
else:
result.set('Left')
else:
if io27:
result.set('Right')
else:
result.set('None')
id = root.after( DELAY, on_after) # This implements the loop by calling on_after again.
id = root.after( 1, on_after) # Start the loop.
root.mainloop()
我正在尝试创建一个程序,它使用 BCM 设置模式简单地读取 2 个引脚,在本例中为引脚 17 和引脚 27。 根据每个引脚的值(0 或 1),程序将在屏幕上显示不同的图像。
我的问题是,我第一次 运行 程序显示的图像是正确的,但如果同时我将任何引脚设置为不同的状态 (ON/OFF),程序不会' 在屏幕上上传正确的图像,显示错误消息。
如果我只是尝试 运行 "prints",它工作正常,但对于图像我总是有这个问题。我从不在第一张图片之后就通过。整个程序都停留在初始图像上。我在 create_image return self._create.
中收到错误编辑:下面 link 中的错误消息图片
error i get https://i.imgur.com/50BxkFp.png
import Tkinter as tk
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.IN)
GPIO.setup(27,GPIO.IN)
app = tk.Tk()
app.attributes("-fullscreen", True)
app.title('Presence State')
screen_width = app.winfo_screenwidth()
screen_height = app.winfo_screenheight()
emptyP = tk.PhotoImage(file = "./images/emptyPresence.jpg")
leftP = tk.PhotoImage(file = "./images/leftPresence.jpg")
rightP = tk.PhotoImage(file = "./images/rightPresence.jpg")
bothP = tk.PhotoImage(file = "./images/bothSidesPresence.jpg")
fname = tk.Canvas(app, bg = "black" , width = screen_width, height =
screen_height)
def empty():
image = fname.create_image(screen_width/2, screen_height/2, anchor =
tk.CENTER, image = emptyP)
def left():
image = fname.create_image(screen_width/2, screen_height/2, anchor =
tk.CENTER, image = leftP)
def right():
image = fname.create_image(screen_width/2, screen_height/2, anchor =
tk.CENTER, image = rightP)
def both():
image = fname.create_image(screen_width/2, screen_height/2, anchor =
tk.CENTER, image = bothP)
while(1):
if GPIO.input(17) == 0 and GPIO.input(27) == 0:
empty()
time.sleep(.5)
elif GPIO.input(17) == 1 and GPIO.input(27) == 0:
left()
time.sleep(.5)
elif GPIO.input(17) == 0 and GPIO.input(27) == 1:
right()
time.sleep(.5)
else:
both()
time.sleep(.5)
fname.pack()
app.mainloop()
我没有 RaspberryPi,所以我创建了两个 Checkbutton 来模拟 IO 引脚。我已经在标签中显示了结果。修改结果以显示图像应该不会太困难。函数 on_after 每 500 毫秒调用一次自身以代替原来的 sleep(0.5)s
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('400x100')
root.title('"After" Demonstation')
# Set up fake GPIO Pins as Checkbuttons
# IntVars to easily read the state.
gpio17 = tk.IntVar()
gpio27 = tk.IntVar()
# Initialise to zero
gpio17.set(0)
gpio27.set(0)
ttk.Checkbutton(root, variable = gpio17, text = 'GPIO 17', width = 20).grid( row = 0, column = 0)
ttk.Checkbutton(root, variable = gpio27, text = 'GPIO 27', width = 20).grid( row = 0, column = 2)
# I'm using a label for simplicity. Amendments required
# below to cope with images.
result = tk.StringVar()
ttk.Label(root, textvariable = result, width = 20 ).grid(row=1, column=1)
DELAY = 500 # in milliseconds
def on_after():
""" Reads the state of the io pins and sets the result
to the appropriate value. """
# Code to fetch the IO states will be different with real pins.
io17 = gpio17.get() # Get the button states
io27 = gpio27.get()
# Set the result. Will be different with images.
if io17:
if io27:
result.set('Both')
else:
result.set('Left')
else:
if io27:
result.set('Right')
else:
result.set('None')
id = root.after( DELAY, on_after) # This implements the loop by calling on_after again.
id = root.after( 1, on_after) # Start the loop.
root.mainloop()