2

So I am kinda stuck on this one and I am really not sure where to go from here. I am trying to create a tkinter gui to control a Parallax RFID read/write module. So far I have managed to code the tkinter window, and created a button, when pressed it will begin to read a rfid tag. My problem is I do not know how to create a label that will update, so that the tag's memory contents is shown in the tkinter window and not just the console.

This is python 2.7 code.

Here is the code I am using to print the tags contents to console.

    print ("Read App Starting...")

    print ("Reading tag's User Data...")
    for ix in range(3, 31):
      while True:
        ser.write('!RW' + chr(CMD_READ) + chr(ix))    # send command
        buf = ser.read(5)           # get bytes (will block until received)
        if buf[0] == chr(ERR_OK):   # if valid data received with no error, continue
          break
      print ("%2d") % ix, ": ",
      for iy in range(1,len(buf)):  # display data
        sys.stdout.write("%02X" % ord(buf[iy]))
      print("")

I have saved this to a file and am importing it into my tkinter code.

Tkinter app

from tkinter import *
import read_tag


class Window(Frame):


    def __init__(self, master=None):
        Frame.__init__(self, master)                 
        self.master = master
        self.init_window()

    #Creation of init_window
    def init_window(self):

        # changing the title of our master widget      
        self.master.title("GUI")

        # allowing the widget to take the full space of the root window
        self.pack(fill=BOTH, expand=1)

        # creating a button instance
        readbutton = Button(self, text="Read Tag", command=read_tag.main)

        # placing the button on my window
        readbutton.place(x=0, y=0)

        outputlabel = Label(self, text="Tag Data :")

        outputlabel.place(x=60, y=0)

        output = Label(self, command=?)

        output.place(x=70, y=0)

root = Tk()

#size of the window
root.geometry("400x300")

app = Window(root)
root.mainloop()  
  • if you want to use `print()` and send it to Tkinter then you will have to assign own class to `sys.stdout` and this class need method `write()` which send text to `Label` (`output["text"] = output["text"] + "new text"`) – furas Apr 14 '19 at 06:10
  • to use script in `tkinter` you may have to use `threading` because script use `while True` loop which will stop `mainloop()` and window will freeze. Or you will have to use `root.after(time, function_name)` instead of `while True`. In both situations you need code in function in script. – furas Apr 14 '19 at 06:16
  • my examples: [how to use after() to read from serial port](https://github.com/furas/python-examples/blob/master/tkinter/read-serial-port/main.py), [how to use threading and queue in tkinter](https://github.com/furas/python-examples/blob/master/tkinter/thread-queue/main%20.py) – furas Apr 14 '19 at 06:19

2 Answers2

0

Use output.config(text='UPDATED TEXT')

0

you can try with listbox in tkinter is easy and clearly, look this example:


from tkinter import *  
  
app = Tk()  
  
app.geometry("200x250")  
  
lbl = Label(app,text = "A list of favourite countries...")  
lbl.pack() 
listbox = Listbox(app)  
listbox.pack()  


listbox.insert(1,"text 1")  
  
listbox.insert(2, "text 2")  
  
listbox.insert(3, "text 3")  
  
listbox.insert(4, "text 4")  
  
 

  
app.mainloop()