Skip to content Skip to sidebar Skip to footer

Update Tkinter Label From Serial Data Whenever There's New Data From Serial Port Python 3.x

I have encounter this problem where i could not display any value on the label which i wanted to constantly update it whenever there's new data coming in from the serial port. I'm

Solution 1:

You can run your GetTemp() method in a seperated thread from _thread module. The thread is called with the Tkinter method after(). In following example I substituted your GetTemp() with a randomn number generated.

import tkinter
import tkinter.messagebox
import time
import random
import _thread

classMenu:

    def__init__(self):    

        self.main_window = tkinter.Tk()
        self.main_window.title("Room Light System")
        self.main_window.geometry("1200x600")


        #Frames
        self.frame_2 = tkinter.Frame(self.main_window, bg='Orange') # Receiving DATAs#ReceiveLabel
        self.ReceiveLabel = tkinter.Label(self.frame_2,\
                                       text = 'Received DATAs',\
                                       bg = 'White',\
                                       height = 2, width = 20)


        #Temperature
        self.GetTempLabel = tkinter.Label(self.frame_2,\
                                       text='Temperature :')
        self.TempValue = tkinter.StringVar()

        self.GetTempValueLabel = tkinter.Label(self.frame_2,bg = 'Green',\
                                               textvariable = self.TempValue
                                               )



        #PACKING!!! F2

        self.frame_2.pack()
        self.frame_2.place(x=410, y=0, height=300, width=400)
        #ReceiveLabel
        self.ReceiveLabel.pack()
        self.ReceiveLabel.place(x=100, y=10)
        #Temperature
        self.GetTempLabel.pack()
        self.GetTempLabel.place(x=50, y=80, height=20, width=120)
        self.GetTempValueLabel.pack()
        self.GetTempValueLabel.place(x=200, y=80, height=20, width=50)


        #main loop and quit
        self.quitButton = tkinter.Button(self.main_window,\
                                          text = 'Quit',
                                          command = self.main_window.destroy,\
                                          height = 2, width = 6)
        self.quitButton.pack() 

        self.quitButton.place(x=200, y=500)


        self.main_window.after(2000, _thread.start_new_thread, self.GetTemp, ())
        tkinter.mainloop()

    defGetTemp(self):

        while(1):
            value = random.random()
            self.TempValue.set(str(value))
            time.sleep(0.5)

gui = Menu()

Solution 2:

call serial inside the method..it will work..here is my code for serial using Mr Holger's concept.. this code is tested with Raspberry Pi3 serial port ttyS0 as master and serially connected sensor.....

import tkinter
import tkinter.messagebox
import time
import _thread
import serial

classMenu:

    def__init__(self):    

        self.main_window = tkinter.Tk()
        self.main_window.title("Serial Data monitor")
        self.main_window.geometry("1000x600")


        #Frames
        self.frame_2 = tkinter.Frame(self.main_window, bg='Orange') # Receiving DATAs#ReceiveLabel
        self.ReceiveLabel = tkinter.Label(self.frame_2,\
                                       text = 'Received DATAs',\
                                       bg = 'White',\
                                       height = 2, width = 20)


        #RPM
        self.GetTempLabel = tkinter.Label(self.frame_2,\
                                       text='RPM :')
        self.TempValue = tkinter.StringVar()

        self.GetTempValueLabel = tkinter.Label(self.frame_2,bg = 'Green',\
                                               textvariable = self.TempValue
                                               )


        #torque
        self.GetTemppLabel = tkinter.Label(self.frame_2,\
                                       text='TORQUE :')
        self.TemppValue = tkinter.StringVar()

        self.GetTemppValueLabel = tkinter.Label(self.frame_2,bg = 'Green',\textvariable = self.TempValue)

PACKING!!! F2

        self.frame_2.pack()
        self.frame_2.place(x=410, y=0, height=300, width=400)
        #ReceiveLabel
        self.ReceiveLabel.pack()
        self.ReceiveLabel.place(x=100, y=10)
        #rpm
        self.GetTempLabel.pack()
        self.GetTempLabel.place(x=50, y=80, height=20, width=80)
        self.GetTempValueLabel.pack()
        self.GetTempValueLabel.place(x=200, y=80, height=20, width=120)

        #torque
        self.GetTemppLabel.pack()
        self.GetTemppLabel.place(x=50, y=120, height=20, width=80)
        self.GetTemppValueLabel.pack()
        self.GetTemppValueLabel.place(x=200, y=120, height=20, width=120)

        #main loop and quit
        self.quitButton = tkinter.Button(self.main_window,\
                                          text = 'Quit',
                                          command = self.main_window.destroy,\
                                          height = 2, width = 6)
        self.quitButton.pack() 

        self.quitButton.place(x=200, y=500)


        self.main_window.after(2000, _thread.start_new_thread, self.GetTemp, ())
        self.main_window.after(4000, _thread.start_new_thread, self.GetTempp, ())
        tkinter.mainloop()

    defGetTemp(self):
        s=serial.Serial(port='/dev/ttyS0',baudrate=9600)
        string='*00T%'whileTrue:
            s.write(str.encode(string))
            print(string)
            time.sleep(2)
            if s.inWaiting():
               temp=s.readline(s.inWaiting())
               value=temp.decode('utf-8')
               value=value[5:-1]
    defGetTempp(self):
        s=serial.Serial(port='/dev/ttyS0',baudrate=9600)
        string1='*01T%'whileTrue:
            s.write(str.encode(string1))
            print(string1)
            time.sleep(2)
            if s.inWaiting():
               tempp=s.readline(s.inWaiting())
               value1=tempp.decode('utf-8')
               value1=value1[5:-1]
               print(value1)
               self.TemppValue.set(str(value1))
               time.sleep(0.5)



gui = Menu()

Post a Comment for "Update Tkinter Label From Serial Data Whenever There's New Data From Serial Port Python 3.x"