Search code examples
pythontkinterrfid

Tkinter + RFID, only display images when RFID tag is read


I have to write a program that displays images when RFID tags are located on the RFID reader and removes the images when the RFID tags are removed from the RFID reader.

The following code can display the images when tag or tag 2 are read, but the continue to display them.

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

The thing that disturbs me is the root.mainloop() function from the Tkinter gui. How can I make the program only display the images when the corresponding tag is read and not display anything when not tag is read?


Solution

  • Lets start with this...

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