Bottom Page

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.
Quote
#2
have you created your basic tkinter app yet?
Quote
#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)
Quote
#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)
Quote
#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?
Quote
#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)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [Tkinter] Display Selected Image from Directory and Resize it EchoLi 0 94 Oct-02-2019, 06:54 PM
Last Post: EchoLi
  Update a label text from subprocess jim53 3 291 Aug-19-2019, 08:21 PM
Last Post: Denni
  Unable to update or refresh label text in tkinter jenkins43 3 292 Jul-24-2019, 02:09 PM
Last Post: Friend
  [Tkinter] What is $DISPLAY supposed to be? amethyst 2 287 Jun-29-2019, 04:36 PM
Last Post: amethyst
  Update value selected in option menu when select an item from text box klllmmm 2 464 Jun-06-2019, 04:51 AM
Last Post: klllmmm
  display confusion anfaenger 2 315 May-06-2019, 07:28 PM
Last Post: Yoriz
  [Tkinter] Having trouble updating the text in a label microphone_head 3 398 May-02-2019, 08:44 AM
Last Post: microphone_head
  [Pyqt5]Display name of new inserted USB stick on GUI Atalanttore 0 449 Mar-24-2019, 08:29 PM
Last Post: Atalanttore
  Display more than one button in GUI to display MPU6000 Sensor readings barry76 4 581 Jan-05-2019, 01:48 PM
Last Post: wuf
  [Tkinter] update the content of Text widget atlass218 10 4,866 Dec-15-2018, 11:51 AM
Last Post: atlass218

Forum Jump:


Users browsing this thread: 1 Guest(s)