swetha25
Posts: 2
Joined: Tue Jan 23, 2018 10:48 am

Read Sensor data and continuously replace the previous data on Tkinter

Wed May 23, 2018 10:15 am

Hello all,
I am trying to display a few sensor data on a display screen using Tkinter. I am really new to this application and I'm struggling :( . For my understanding, I came up with a code that can read the data(but not replace. Just read and displayed on the Terminal) when a button is presses. The data can be read continuously but the loop wont end and my GUI hangs :roll: . I've attached a sample code here which I can't quite interpret.
So, my query is: What should be inside GetTemp definition to read the sensor data and replace with the old data?

Code: Select all

import tkinter as tk

class Mainframe(tk.Frame):
    # Mainframe contains the widgets
    # More advanced programs may have multiple frames
    # or possibly a grid of subframes
    
    def __init__(self,master,*args,**kwargs):
        # *args packs positional arguments into tuple args
        # **kwargs packs keyword arguments into dict kwargs
        
        # initialise base class
        tk.Frame.__init__(self,master,*args,**kwargs)
        # in this case the * an ** operators unpack the parameters
        
        # put your widgets here
        self.Temperature = tk.IntVar()
        tk.Label(self,textvariable = self.Temperature).pack()
        self.TimerInterval = 500
        
        # variable for dummy GetTemp
        self.Temp = 0
        
        # call Get Temp which will call itself after a delay
        self.GetTemp()
        
    def GetTemp(self):
        ## replace this with code to read sensor
        self.Temperature.set(self.Temp)
        self.Temp += 1
        
        # Now repeat call
        self.after(self.TimerInterval,self.GetTemp)
   
class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
               
        # set the title bar text
        self.title('CPU Temperature Demo')
        # Make sure app window is big enough to show title 
        self.geometry('300x100')
      
        # create and pack a Mainframe window
        Mainframe(self).pack()
        
        # now start
        self.mainloop()
                    
# create an App object
# it will run itself
App()
Any help regarding this issue would be very helpful. Thank You for you time :)

User avatar
bensimmo
Posts: 3115
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Read Sensor data and continuously replace the previous data on Tkinter

Wed May 23, 2018 12:46 pm

I don't use it directly but use guizero a python module that aims to make it simpler.

Here are my ramblings on reading SenseHAT sensors
viewtopic.php?f=49&t=202386#p1263308

It might give you some ideas of how it should work.

scotty101
Posts: 3031
Joined: Fri Jun 08, 2012 6:03 pm

Re: Read Sensor data and continuously replace the previous data on Tkinter

Wed May 23, 2018 2:06 pm

A quick search to find "CPU Temp Raspberry Pi Python" finds this

Code: Select all

def get_cpu_temperature():
    """get cpu temperature using vcgencmd"""
    process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
    output, _error = process.communicate()
    return float(output[output.index('=') + 1:output.rindex("'")])
You can add this function to your code and call it in your GetTemp method like this

Code: Select all

def GetTemp(self):
        ## replace this with code to read sensor
        self.Temp = get_cpu_temperature()
        self.Temperature.set(self.Temp)
        
        # Now repeat call
        self.after(self.TimerInterval,self.GetTemp)
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

rickyh89
Posts: 55
Joined: Mon Jun 18, 2018 11:01 pm

Re: Read Sensor data and continuously replace the previous data on Tkinter

Tue Jun 19, 2018 1:51 am

Cleaned it up for you so can easily use one class with Tkinter for you app.

also fixed your issue. python 2.7

Code: Select all

import tkinter as tk
   
class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.title('CPU Temperature Demo')
        self.geometry('300x100')
        self.Temp = 0
        self.TimerInterval = 500
        self.labels()
        self.buttons()
        
    def labels(self):
        self.Temperature = tk.IntVar()
        tk.Label(self,textvariable = self.Temperature).pack()
        self.TimerInterval = 500

    def buttons(self):
        get_temp_button = tk.Button(self,text='Start temp display',
                                    command=lambda:self.GetTemp())
        stop_temp_button = tk.Button(self,text='Stop temp display',
                                     command=lambda:self.StopTemp())
        get_temp_button.pack()
        stop_temp_button.pack()

    def GetTemp(self):
        ## replace this with code to read sensor
        self.Temperature.set(self.Temp)
        self.Temp += 1
        
        # Now repeat call
        self.var = self.after(self.TimerInterval,self.GetTemp)

    def StopTemp(self):
        if self.var is not 0:
            self.after_cancel(self.var)
            self.var = 0
        
if __name__ == "__main__":
    
    app = App()
    app.mainloop()

Return to “Python”

Who is online

Users browsing this forum: No registered users and 15 guests