Python Forum
Display and update the label text which display the serial value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Display and update the label text which display the serial value
#1
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.
Reply
#2
have you created your basic tkinter app yet?
Reply
#3
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)
Reply
#4
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)
Reply
#5
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?
Reply
#6
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read text in kivy textinput or Label jadel440 1 151 Dec-29-2020, 10:47 AM
Last Post: joe_momma
  [Tkinter] Display IP address in listbox dominicrsa 2 168 Oct-21-2020, 01:00 PM
Last Post: dominicrsa
  How to display results from terminal window onto tkinter. buttercup 0 303 Jul-21-2020, 04:41 AM
Last Post: buttercup
  Display text 3 words at a time algae 5 638 Jun-27-2020, 10:25 AM
Last Post: menator01
  [Kivy] Kivy text label won't shows up! AVD_01 1 709 Jun-21-2020, 04:01 PM
Last Post: AVD_01
  [PyQt] display content from left to right in QComboBox or QLineEdit mart79 2 454 May-30-2020, 04:38 PM
Last Post: Axel_Erfurt
  [Tkinter] Python 3 change label text gw1500se 6 1,208 May-08-2020, 05:47 PM
Last Post: deanhystad
  QComboBox doesn't display selection as long as it has not lost the focus Meaulnes 3 408 May-07-2020, 03:42 PM
Last Post: Meaulnes
  [Tkinter] Problems to display Web Scraping values in Tkinter Lucas_Ribeiro 0 387 May-07-2020, 12:36 AM
Last Post: Lucas_Ribeiro
  [Tkinter] how to update label text from list Roshan 8 875 Apr-25-2020, 08:04 AM
Last Post: Roshan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020