1

I'm making a gui for my rfid reader but when i run the program it only operates on the shell window and does not open a tkinter gui window. I'm kind of new with python and it's gui so can anybody help me. even a tutorial for a solution to my problem can help. here is my code. thank you for the help.

from tkinter import *
import binascii
import socket
import time
import signal
import sys

import Adafruit_PN532 as PN532

root = Tk(className="Team Baboy") 
root.geometry("800x480")
welcome = Label(root,text="Welcome to Hog Traceability System")
welcome.pack()
welcome.config(font=("Gothic", 18))
back = Frame(width=800, height=480)
back.pack()


instruction = Label(master=back, text='Tap your Authorization Card')
instruction.pack()
instruction.config(font=("Gothic", 30))
instruction.grid(row=0, column=1, padx=0, pady=150)

CS   = 18
MOSI = 23
MISO = 24
SCLK = 25

CARD_KEY = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]

Delay = 1

HEADER = b'BG'

def close(signal, frame):
         sys.exit(0)

signal.signal(signal.SIGINT, close)

pn532 = PN532.PN532(cs=CS, sclk=SCLK, mosi=MOSI, miso=MISO)
pn532.begin()
pn532.SAM_configuration()

print('PN532 NFC RFID 13.56MHz Card Reader')

while True:
    uid = pn532.read_passive_target()
    if uid is None:
        continue
    print('')
    print('Card UID 0x{0}'.format(binascii.hexlify(uid)))
    if not pn532.mifare_classic_authenticate_block(uid, 4, PN532.MIFARE_CMD_AUTH_B, CARD_KEY):
        print('Failed to authenticate with card!')
        continue
    data = pn532.mifare_classic_read_block(4)
    if data is None:
        print('Failed to read data from card!')
        continue
    if data[0:2] !=  HEADER:
        print('Card is not written with proper block data!')
        continue
    print('User Id: {0}'.format(int(data[2:8].decode("utf-8"), 16)))




root.mainloop() 
  • Which version of python are you using? For versions earlier than python3 it is Tkinter, but in Python 3 it is tkinter – James Geddes Apr 07 '18 at 08:20

1 Answers1

0

This is likely a tkinter issue, rather than one related to RFID. I would start by getting the GUI to work before adding RFID functionality.

Here, I have commented out all the RFID code to leave just the tkinter code. I then initialised Tk() without arguments, as per the documentation.

from Tkinter import *
import binascii
import socket
import time
import signal
import sys

# import Adafruit_PN532 as PN532

root = Tk()
root.className = "TeamBadoy"
# root = Tk(className="Team Baboy")
root.geometry("800x480")
welcome = Label(root,text="Welcome to Hog Traceability System")
welcome.pack()
welcome.config(font=("Gothic", 18))
back = Frame(width=800, height=480)
back.pack()


instruction = Label(master=back, text='Tap your Authorization Card')
instruction.pack()
instruction.config(font=("Gothic", 30))
instruction.grid(row=0, column=1, padx=0, pady=150)

CS   = 18
MOSI = 23
MISO = 24
SCLK = 25

# CARD_KEY = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]

Delay = 1

HEADER = b'BG'

# def close(signal, frame):
#          sys.exit(0)
#
# signal.signal(signal.SIGINT, close)
#
# pn532 = PN532.PN532(cs=CS, sclk=SCLK, mosi=MOSI, miso=MISO)
# pn532.begin()
# pn532.SAM_configuration()

print('PN532 NFC RFID 13.56MHz Card Reader')

# while True:
    # uid = pn532.read_passive_target()
    # if uid is None:
    #     continue
    # print('')
    # print('Card UID 0x{0}'.format(binascii.hexlify(uid)))
    # if not pn532.mifare_classic_authenticate_block(uid, 4, PN532.MIFARE_CMD_AUTH_B, CARD_KEY):
    #     print('Failed to authenticate with card!')
    #     continue
    # data = pn532.mifare_classic_read_block(4)
    # if data is None:
    #     print('Failed to read data from card!')
    #     continue
    # if data[0:2] !=  HEADER:
    #     print('Card is not written with proper block data!')
    #     continue
    # print('User Id: {0}'.format(int(data[2:8].decode("utf-8"), 16)))




root.mainloop()
# while True:
#     tk.update_idletasks()
#     tk.update()
#     sleep(0.01)

This results in a GUI window displaying; it does not actually do anything yet, but it does show up!

Screenshot of GUI window produced by above code

On Tkinter understanding mainloop the suggestion is that, rather than using mainloop(), you might want to consider explicitly stating;

while True:
    root.update_idletasks()
    root.update()
    sleep(0.01)
James Geddes
  • 742
  • 3
  • 10
  • 35
  • Actually, if you can, you should use the Tkinter event loop. It forces the developer to write event-driven code, which is easier to extend (using [`after`](https://www.tcl.tk/man/tcl8.5/TclCmd/after.htm), for example). – Daniel Apr 07 '18 at 14:14