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
  [Tkinter] Update label if there are no records in treeview TomasSanchexx 1 907 Aug-20-2023, 04:45 PM
Last Post: menator01
  [PyQt] PyQt5 window closing when trying to display a graph bianca 4 1,622 Aug-12-2023, 03:25 PM
Last Post: bianca
  [PyQt] [solved] How to display a pdf-file in a PyQt6 widget BigMan 13 15,680 May-06-2023, 09:27 AM
Last Post: Axel_Erfurt
  [Tkinter] Can't update label in new tk window, object has no attribute tompranks 3 3,467 Aug-30-2022, 08:44 AM
Last Post: tompranks
  [PyQt] [Solved]Display PyQtTable results from A->Z & Z->A Extra 2 1,143 Jul-18-2022, 04:04 PM
Last Post: Extra
  [PyQt] [Solved]Display Search Results in QTable Extra 5 2,387 Jun-29-2022, 10:20 PM
Last Post: Extra
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,672 Jun-26-2022, 06:26 PM
Last Post: menator01
  [PyQt] Cannot Display Image after Selecting Image bintangkecil 4 2,502 Jun-12-2022, 08:18 AM
Last Post: Axel_Erfurt
  looking for scripts that do simple image display Skaperen 10 4,134 Sep-13-2021, 05:35 PM
Last Post: FullOfHelp
  [Tkinter] Make my button text update? Skata100 1 2,013 Aug-07-2021, 05:37 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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