Python Forum
Display and update the label text which display the serial value - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Display and update the label text which display the serial value (/thread-15832.html)



Display and update the label text which display the serial value - jenkins43 - Feb-02-2019

Hi, With help of mentioned below code I have been able to get the result mentioned in below
import serial

ard = serial.Serial('COM4', 9600);
while True:
    k = ard.readline().decode('ascii') 
    c = k.split('|')
    print(c)
['19.14', '66.44', '83.87', '\r\n']
['19.08', '66.34', '87.49', '\r\n']
['19.11', '66.40', '84.07', '\r\n']
['19.12', '66.42', '82.78', '\r\n']
['19.15', '66.47', '87.69', '\r\n']
['19.12', '66.42', '87.03', '\r\n']
['19.14', '66.44', '88.17', '\r\n']
But I need to display c[0], c[1], c[3] as label text in Tkinter and want to update all the label text after a certain time
Being new to I need some advice on making it work. Your leads are always valuable to me.


RE: Display and update the label text which display the serial value - Larz60+ - Feb-02-2019

have you created your basic tkinter app yet?


RE: Display and update the label text which display the serial value - jenkins43 - Feb-03-2019

I have created the application which is receiving the data from Arduino but was using a message box to show the data but I need to know about how can display the data in label text and update it whenever I get new data

import serial
import json
from tkinter import messagebox
from tkinter import *
import tkinter as ttk
import serial.tools.list_ports

ard = serial.Serial();
root = ttk.Tk()
root.title("Read Sensor")

B = None

# Add a grid
mainframe = Frame(root)
mainframe.grid(column=0,row=0, sticky=(N,W,E,S) )
mainframe.columnconfigure(0, weight = 1)
mainframe.rowconfigure(0, weight = 1)
mainframe.pack(pady = 30, padx = 50)
 
# Create a Tkinter variable
tkvar1 = StringVar(root)
tkvar = StringVar(root)


#Serial Port
a=serial.tools.list_ports.comports()
ports = [port.device for port in a]
print(ports)
#tkvar1.set(ports[0])
    
# Dictionary with options
baud = { '9600','119200','34800'}

#Pop Up desciption
popupMenu1 = OptionMenu(mainframe, tkvar1, *ports)
Label(mainframe, text="Serial Port").grid(row = 1, column = 1)
popupMenu1.grid(row = 2, column =1)

popupMenu2 = OptionMenu(mainframe, tkvar, *baud)
Label(mainframe, text="Baudrate").grid(row = 3, column = 1)
popupMenu2.grid(row = 4, column =1)


#Serial Callback Functions
def helloCallBack():
    k = ard.readline().decode('ascii')
    c = k.strip('|')
    #messagebox.showinfo('Message From Arduino',k)
    g1 = Label(mainframe, text="Temperature Output in C", padx = 30, anchor=NE).grid(row = 1, column = 4)
    #g2 = Label(mainframe, textvariable = c[0], padx = 30, anchor=NE).grid(row = 1, column = 5)
    g3 = Label(mainframe, text="Temperature Output in F", padx = 30, anchor=E).grid(row = 2, column = 4)
    #g4 = Label(mainframe, textvariable = c[1], padx = 30, anchor=NE).grid(row = 2, column = 5)
    g5 = Label(mainframe, text="Humidity", padx = 30, anchor = SE).grid(row = 3, column = 4)
    #g6 = Label(mainframe, textvariable = c[2], padx = 30, anchor=NE).grid(row = 3, column = 5)

def start_stop(root, btn_text, helloCallBack):
    first = True
    def call():
        nonlocal first
        if first:
            helloCallBack()
            first = False
            btn_text.set('Disconnect')
        else:
            root.destroy()
    return call


#baudrate functions
def dropCall(*args):
   ard.port = tkvar1.get()
   ard.open()
   print(tkvar1.get)
   global B
   value = tkvar.get()
   baud_dict={'9600':value, '34800':value, '119200':value}
   if value in baud_dict:
      ard.baudrate = int(baud_dict[value])
      print(ard.baudrate)
      if B:
         B.destroy()
      btn_text = ttk.StringVar()
      B = ttk.Button(root, textvariable=btn_text, command = start_stop(root, btn_text, helloCallBack))
      btn_text.set('Connect')
      B.pack()

#Link Function
tkvar.trace('w', dropCall)



RE: Display and update the label text which display the serial value - Larz60+ - Feb-03-2019

You need to set up textvariable=varname in each label that you want to display info in
each of these backed up by a StringVar
then set value of stringvar to info you want displayed:
example:
g1txt = StringVar()
g1txt.set('message from arduino') # This needs to be updated with data from each serial read
g1 = Label(mainframe, text="Temperature Output in C", textvariable=g1txt, padx = 30, anchor=NE).grid(row = 1, column = 4)



RE: Display and update the label text which display the serial value - jenkins43 - Feb-03-2019

Ok so I have been working with a small example and I tried the way you told me as follows

import tkinter as tk


active_counter = False


def count():
    if active_counter:
        counter = ['28.34', '27.34', '26.34', '28.54', '22.34', '25.34', '29.34']
        label1 = StringVar(counter)
        label1.set(counter[0])
        label.config(textvariable = coutner[])
        g1 = Label(mainframe, text="Temperature Output in C", textvariable=g1txt, padx = 30, anchor=NE).grid(row = 1, column = 4)
        label.after(1000, count)


def start_stop():
    global active_counter
    if button['text'] == 'Start':
        active_counter = True
        count()
        button.config(text="Stop")
    else:
        active_counter = False
        button.config(text="Start")


root = tk.Tk()
root.title("Counting Seconds")
label = tk.Label(root, fg="green")
label.pack()
button = tk.Button(root, text='Start', width=25, command=start_stop)
button.pack()
root.mainloop()
but yet again I am facing this error in which StringVar is not defined

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\jenkins\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
  File "C:\Users\jenkins\Desktop\test\Label Box.py", line 21, in start_stop
    count()
  File "C:\Users\jenkins\Desktop\test\Label Box.py", line 10, in count
    label1 = StringVar(counter[0])
NameError: name 'StringVar' is not defined
I might be doing silly mistake which I am not able to find out
Will it then update and display array data one by one or in a single string only?


RE: Display and update the label text which display the serial value - Larz60+ - Feb-04-2019

this is wrong
label1 = StringVar(counter[0])
look at my previous post again.
definition of string variable needs to be:
name = StringVar()
to set value, use
name.set(value)