简体   繁体   English

如何在 tkinter 中显示传感器读数?

[英]How to show sensor reading in tkinter?

I am trying to capture readings from the sensor into tkniter and am stuck on a small problem.我正在尝试将传感器的读数捕获到 tkniter 中,但遇到了一个小问题。 To simulate sensor reading I created a small function that increments the number for 1 every second.为了模拟传感器读数,我创建了一个小函数,每秒将数字递增 1。 And in this example, I am trying to present that counter within tkniter label.在这个例子中,我试图在 tkniter 标签中展示那个计数器。

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.如果设置我的 self.temperature.set(5) 我会看到 tkineter 中显示的是 5 摄氏度。 However, when I try dynamically feeding this variable using temp() function, I am not getting any numbers.但是,当我尝试使用 temp() 函数动态输入此变量时,我没有得到任何数字。

What I expect is to see 1, then 2, then 3 etc.我希望看到 1,然后是 2,然后是 3 等等。

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)

You update the temperature variable by calling temperature.set(...) ,您通过调用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.然后您可以使用self.after(...)方法通过再次调用self.temp()函数以固定时间间隔更新变量。

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

With Alexander's help here, I was able to incorporate readings from BME680 sensor and project them into tknite GUI.在 Alexander 的帮助下,我能够合并来自 BME680 传感器的读数并将它们投射到 tknite GUI 中。 Here is the code if anyone encounters the 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="Pressure")
        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):
            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()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM