无法通过GPIO引脚更新图像

时间:2019-07-05 18:45:23

标签: python-2.7 tkinter raspberry-pi2 gpio

我正在尝试创建一个程序,该程序使用BCM设置模式简单地读取2个引脚(在这种情况下为引脚17和27)。 根据每个引脚的值(0或1),程序将在屏幕上显示不同的图像。

我的问题是,我第一次运行该程序时,它显示的图像是正确的图像,但是如果同时我将任何引脚设置为不同的状态(开/关),则该程序不会在该图像上载正确的图像。屏幕,显示一条错误消息。

如果我只是尝试运行“打印件”,则效果很好,但是对于图像,我始终会遇到此问题。我从没有经过第一张照片。所有程序都会被初始映像卡住。我在create_image返回self._create时遇到错误。

编辑:下面链接中的错误消息图像

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()

1 个答案:

答案 0 :(得分:0)

我没有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()