我必须编写一个程序,当RFID标签位于RFID阅读器上时显示图像,并在RFID标签从RFID阅读器中取出时删除图像。
以下代码可以在读取标签或标签2时显示图像,但继续显示它们。
from Tkinter import *
import serial
import time
one = '0419AC8E70'
two = '0419ACB481'
ser = serial.Serial('/dev/ttyUSB0', 2400)
reader = ser.read(11)
root = Tk()
frame = Frame(root)
frame.pack()
photo = PhotoImage(file="/home/daniel/Desktop/BY/test3.gif")
photo2 = PhotoImage(file="/home/daniel/Desktop/BY/test2.gif")
firstimage = Label(frame, image=photo)
secondimage = Label(frame, image=photo2)
def set_image():
if one in reader:
print("1")
ser.flush()
time.sleep(1)
firstimage.pack( fill = BOTH)
else:
firstimage.pack_forget()
if two in reader:
print("1")
secondimage.pack( fill = BOTH)
ser.flush()
time.sleep(1)
else:
secondimage.pack_forget()
firstimage.after(200,set_image) #to run set_image function at regular intervals
set_image()
root.mainloop()
令我不安的是来自Tkinter gui的root.mainloop()函数。如何使程序仅在读取相应标签时显示图像,而在未读取标签时不显示任何内容?
答案 0 :(得分:0)
让我们从这开始......
from Tkinter import *
import serial
import time
one = '0419AC8E70'
two = '0419ACB481'
ser = serial.Serial('/dev/ttyUSB0', 2400)
reader = ser.read(11)
root = Tk()
frame = Frame(root)
frame.pack()
#photo = PhotoImage(file="/home/daniel/Desktop/BY/test3.gif")
#photo2 = PhotoImage(file="/home/daniel/Desktop/BY/test2.gif")
firstimage = Label(frame)
firstimage.pack()
secondimage = Label(frame)
secondimage.pack()
def set_image():
ser = serial.Serial('/dev/ttyUSB0', 2400)
reader = ser.read(11)
if one in reader:
firstimage.config(text="One in reader")
elif two in reader:
secondimage.config(text="Two in reader")
else:
firstimage.config(text="One not in reader")
secondimage.config(text="Two not in reader")
firstimage.after(2000,set_image)
secondimage.after(2000,set_image)
set_image()
root.mainloop()