How to show sensor reading in tkinter?

Question:

I am trying to capture readings from the sensor into tkniter and am stuck on a small problem.
To simulate sensor reading I created a small function that increments the number for 1 every second. And in this example, I am trying to present that counter within tkniter label.

Here is the code:

import tkinter
import customtkinter

# Setting up theme of GUI
customtkinter.set_appearance_mode("Dark")  # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("blue")  # Themes: "blue" (standard), "green", "dark-blue"

class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()

        # configure window
        self.is_on = True
        self.title("Cool Blue")
        self.geometry(f"{220}x{160}")
        self.temperature = tkinter.IntVar()

        # configure grid layout (4x4)
        self.grid_columnconfigure(1, weight=1)

        # create frame for environmental variable
        self.temperature_frame = customtkinter.CTkFrame(self)
        self.temperature_frame.grid(row=0, column=1, rowspan = 1, padx=(5, 5), pady=(10, 10), sticky="n")
        self.temperature_frame.grid_rowconfigure(2, weight=1)
        self.label_temperature = customtkinter.CTkLabel(master=self.temperature_frame, text="Temperature")
        self.label_temperature.grid(row=0, column=1, columnspan=2, padx=10, pady=10, sticky="")
        self.label_temperature_value = customtkinter.CTkLabel(master=self.temperature_frame,
                                                              textvariable=self.temperature,
                                                              font=customtkinter.CTkFont(size=50, weight="bold"))

        self.label_temperature_value.grid(row=1, column=1, columnspan=1, padx=10, pady=10, sticky="e")
        self.label_temperature_value = customtkinter.CTkLabel(master=self.temperature_frame,
                                                              text = f'N{DEGREE CELSIUS}',
                                                              font=customtkinter.CTkFont(size=30, weight="bold"))

        self.label_temperature_value.grid(row=1, column=3, columnspan=1, padx=(10, 10), pady=10, sticky="sw")

    def temp(self):
        import time
        i = 0
        while True:
            start = round(time.time(), 0)
            time.sleep(1)
            stop = round(time.time(), 0)
            j = stop - start
            i = i + j
            print(i)
        return i

        self.temperature = temp(self)


if __name__ == "__main__":
    app = App()
    app.mainloop()

If set my self.temperature.set(5) I see the 5 Celsius displayed within tkineter.
However, when I try dynamically feeding this variable using temp() function, I am not getting any numbers.

What I expect is to see 1, then 2, then 3 etc.

What am I doing wrong here?

thank you in advance.

PS. here is the example of my code for reading the data from the sensor:

import time
import board
from busio import I2C
import adafruit_bme680
import datetime
import adafruit_veml7700

i2c = board.I2C()  # uses board.SCL and board.SDA
veml7700 = adafruit_veml7700.VEML7700(i2c)
# Create library object using our Bus I2C port
i2c = I2C(board.SCL, board.SDA)
bme680 = adafruit_bme680.Adafruit_BME680_I2C(i2c, debug=False)

while True:
    TEMPERATURE = round(bme680.temperature, 2)
    time. Sleep(1)
Asked By: Slavisha84

||

Answers:

You update the temperature variable by calling temperature.set(...),

Then you can use the self.after(...) method to update the variable at intervals of some fixed amount of time by calling the self.temp() function again.

At the bottom I included a dummy class that changes temperature to simulate getting a signal from external source.

Example:

import time
import tkinter
import customtkinter

# Setting up theme of GUI
customtkinter.set_appearance_mode("Dark")  # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("blue")  # Themes: "blue" (standard), "green", "dark-blue"

class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        # configure window
        self.is_on = True
        self.title("Cool Blue")
        self.geometry(f"{220}x{160}")
        self.temperature = tkinter.IntVar()
        # configure grid layout (4x4)
        self.grid_columnconfigure(1, weight=1)
        # create frame for environmental variable
        self.temperature_frame = customtkinter.CTkFrame(self)
        self.temperature_frame.grid(row=0, column=1, rowspan = 1, padx=(5, 5), pady=(10, 10), sticky="n")
        self.temperature_frame.grid_rowconfigure(2, weight=1)

        self.label_temperature = customtkinter.CTkLabel(master=self.temperature_frame, text="Temperature")
        self.label_temperature.grid(row=0, column=1, columnspan=2, padx=10, pady=10, sticky="")

        self.label_temperature_value = customtkinter.CTkLabel(
            master=self.temperature_frame, textvariable=self.temperature,
            font=customtkinter.CTkFont(size=50, weight="bold"))
        self.label_temperature_value.grid(row=1, column=1, columnspan=1, padx=10, pady=10, sticky="e")

        self.label_temperature_value = customtkinter.CTkLabel(
            master=self.temperature_frame, text = f'N{DEGREE CELSIUS}',
            font=customtkinter.CTkFont(size=30, weight="bold"))
        self.label_temperature_value.grid(row=1, column=3, columnspan=1, padx=(10, 10), pady=10, sticky="sw")

        self.temp()  # call the temp function just once

    def temp(self):
        self.temperature.set(Temp.current()) 
        self.after(2000, self.temp)  # 2000 milliseconds = 2 seconds

import random
class Temp:
    temp = random.randint(10,35)
    def current():
        diff = random.randint(-3,3)
        return Temp.temp + diff

if __name__ == "__main__":
    app = App()
    app.mainloop()
Answered By: Alexander

With Alexander’s help here, I was able to incorporate readings from BME680 sensor and project them into tknite GUI.
Here is the code if anyone encounter same issue:

import time
import tkinter
import customtkinter

import time
import board
from busio import I2C
import adafruit_bme680

# Create library object using our Bus I2C port
i2c = I2C(board.SCL, board.SDA)
bme680 = adafruit_bme680.Adafruit_BME680_I2C(i2c, debug=False)

# Setting up theme of GUI
customtkinter.set_appearance_mode("Dark")  # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("blue")  # Themes: "blue" (standard), "green", "dark-blue"

class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        # configure window
        self.is_on = True
        self.title("Cool Blue")
        self.geometry(f"{220}x{160}")
        self.temperature = tkinter.IntVar()
        self.pressure = tkinter.IntVar()
        # configure grid layout (4x4)
        self.grid_columnconfigure(1, weight=1)
        # create frame for environmental variable
        self.temperature_frame = customtkinter.CTkFrame(self)
        self.temperature_frame.grid(row=0, column=1, rowspan = 1, padx=(5, 5), pady=(10, 10), sticky="n")
        self.temperature_frame.grid_rowconfigure(2, weight=1)

        self.label_temperature = customtkinter.CTkLabel(master=self.temperature_frame, text="Temperature")
        self.label_temperature.grid(row=0, column=1, columnspan=2, padx=10, pady=10, sticky="")

        self.label_temperature_value = customtkinter.CTkLabel(master=self.temperature_frame, textvariable=self.temperature,font=customtkinter.CTkFont(size=50, weight="bold"))
        self.label_temperature_value.grid(row=1, column=1, columnspan=1, padx=10, pady=10, sticky="e")

        self.label_temperature_value = customtkinter.CTkLabel(master=self.temperature_frame, text = f'N{DEGREE CELSIUS}',font=customtkinter.CTkFont(size=30, weight="bold"))
        self.label_temperature_value.grid(row=1, column=3, columnspan=1, padx=(10, 10), pady=10, sticky="sw")


        self.pressure_frame = customtkinter.CTkFrame(self)
        self.pressure_frame.grid(row=0, column=2, rowspan = 1, padx=(5, 5), pady=(10, 10), sticky="n")
        self.pressure_frame.grid_rowconfigure(2, weight=1)

        self.label_pressure = customtkinter.CTkLabel(master=self.pressure_frame, text="Temperature")
        self.label_pressure.grid(row=0, column=1, columnspan=2, padx=10, pady=10, sticky="")

        self.label_pressure_value = customtkinter.CTkLabel(master=self.pressure_frame, textvariable=self.pressure,font=customtkinter.CTkFont(size=50, weight="bold"))
        self.label_pressure_value.grid(row=1, column=1, columnspan=1, padx=10, pady=10, sticky="e")

        self.label_pressure_value = customtkinter.CTkLabel(master=self.temperature_frame, text = f'N{DEGREE CELSIUS}',font=customtkinter.CTkFont(size=30, weight="bold"))
        self.label_pressure_value.grid(row=1, column=3, columnspan=1, padx=(10, 10), pady=10, sticky="sw")



        self.temp()  # call the temp function just once

    def temp(self):
        current_temp, current_pres,  = self.current()
        self.temperature.set(current_temp)
        self.pressure.set(current_pres)
        self.after(2000, self.temp)  # 2000 milliseconds = 2 seconds

    def current(self):
            while True:
                current_temp = round(bme680.temperature, 2)
                current_pres = round(bme680.pressure, 2)
                print(f'{current_temp}')
                return current_temp, current_pres

if __name__ == "__main__":
    app = App()
    app.mainloop()

enter image description here

Answered By: Slavisha84
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.