Home > Enterprise >  How to show sensor reading in tkinter?
How to show sensor reading in tkinter?

Time:12-15

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)

CodePudding user response:

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()
  • Related