【发布时间】: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