【问题标题】:Can't update images changing GPIO pins无法更新更改 GPIO 引脚的图像
【发布时间】:2019-07-05 18:45:23
【问题描述】:

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

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

如果我只是尝试运行“打印”,它可以正常工作,但是对于图像,我总是会遇到这个问题。我从来没有在第一张图片之后立即通过。被初始图像卡住了所有程序。我在 create_image return self._create 中遇到错误。

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

error i gethttps://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()

【问题讨论】:

  • 搜索“[tkinter] sleep”会有所帮助。该应用程序还将每秒两次向画布添加图像。图片需要刷新/修改,不添加。
  • @TlsChris 首先感谢您的回答。其次,我不确定这是否是问题所在,但我读到“睡眠”完全卡在后台发生的任何操作中。所以这意味着如果我在显示图像的同时更改引脚,他那时不会读取引脚,对吗?第三,我一直在阅读有关如何更新图像的信息,并且一直在寻找诸如“after”或“update_idletasks”之类的解决方案,但是这两个功能中的任何一个都需要agr才能返回到以前的功能。但我没有看到我应该在我的程序中回忆的地方:S

标签: python-2.7 tkinter raspberry-pi2 gpio


【解决方案1】:

我没有 RaspberryPi,所以我创建了两个 Checkbuttons 来模拟 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()

【讨论】:

  • 非常感谢@Tls Chris 重做我的代码,将 while(1) 转换为函数 main 并使用 after() 方法,运行良好并且知道我可以更新图像。但是我遇到了 GPIO 引脚的新问题,但我可能不得不在新帖子中提问,因为它不再与 Tkinter 相关。但再次感谢你。你很有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多