可能导致我的 Python GUI 程序中的“类型对象”MainFrame“没有属性”MainTemp_meter1“错误的原因

What might be causing 'type object 'MainFrame' has no attribute 'MainTemp_meter1'' error in my Python GUI program

提问人:Anton Wedzinga 提问时间:6/1/2023 更新时间:6/1/2023 访问量:14

问:

我正在编写一个代码来更新我的温度传感器和一个计时器,并将其转换为我的 Gui 上的仪表。问题出在更新程序代码中的某个地方,因为这是创建错误代码的唯一代码,更准确地说,这个错误代码:“类型对象'MainFrame'没有属性'MainTemp_meter1'”。

我尝试了不同的代码和代码样式来使更新程序正常工作,但到目前为止都没有工作。现在有了最新的,我至少可以保持 Gui 运行,但我还不能更新任何内容。 [使用的进口]

import tkinter as tk
import customtkinter as ctk
import threading
from tkdial import Meter
from PIL import Image
import datetime as dt
from random import *
import threading

#references to other .py files
import timers
import relays
import sensors
import logs
import time

[更新程序代码]

def update_readings(self):
            try:
                while True:
                    # Update temperature reading 
                    temp_reading_top = sensors.sensor.read_temperature()
                    humid_reading_top = sensors.sensor.read_humidity()
                    MainFrame.MainTemp_meter1.set(temp_reading_top)
                    DiagFrame.temp_meter1.set(temp_reading_top)
                    DiagFrame.humid_meter1.set(humid_reading_top)

                    # Update current date and time
                    date = dt.datetime.now()
                    MainFrame.label_timedate.configure(text=f"{date:%A, %B %d, %Y, %H:%M}")

                    # Update timer label 
                    timer_reading = self.timer_frame.timer10.read_current()
                    formatted_time = timers.Timer.format_time(timer_reading)  # Call format_time from timers.Timer
                    self.timer_frame.time_var.set(formatted_time)

                    print('updated')

                    # Update the GUI
                    MainFrame.update_idletasks()
                    DiagFrame.update_idletasks()
                    TimerFrame.update_idletasks()
                    # Delay for a certain period (e.g., 1 second)
                    time.sleep(1)
            except Exception as e:
                print("Update error:", str(e))
    # Create and start a separate thread for the update loop
    update_thread = threading.Thread(target=update_readings, daemon=True)
    update_thread.start()

到目前为止,我一直希望更新程序在线并使我的代码正常工作,以便我可以随心所欲地监视不同的情况。使用的一些例子是测量内部或外部的温度以及湿度,并将其放在日志中,这样我就可以看到凌晨 3 点左右的温度。

[代码大型机]

class MainFrame(ctk.CTkFrame):
    def __init__(self, master, **kwargs):
        super().__init__(master, **kwargs)
        self.MainTemp_meter1 = tk.StringVar()
        # Code for temperature gauges...
        #temp_reading_top = sensors.sensor.read_temperature()
        #temp_reading_bottom = sensors.sensor.read_temperature()

        # Code for humidity gauges...
        #humid_reading_top = sensors.read_humidity()
        #humid_reading_bottom = sensors.read_humidity()

        date = dt.datetime.now()
        # create label to display
        self.label_timedate = ctk.CTkLabel(self, text=f"{date:%A, %B %d, %Y, %H:%M}")
        self.label_timedate.grid(row=0, column=0, pady=10)

        self.grid_columnconfigure(2, weight=1)
        # add widgets onto the frame, for example:
        self.label = ctk.CTkLabel(self, text="Status")
        self.label.grid(row=1, column=0, padx=10)

        self.MainTemp_meter1 = Meter(self, radius=200, start=0, end=70, border_width=0, fg="#242424", bg="#242424",
               text_color="white", start_angle=270, end_angle=-270,
               text_font="DS-Digital 12", scale_color="white", needle_color="red", text=" \N{DEGREE SIGN}C")
        self.MainTemp_meter1.set_mark(50, 70) # set red marking from 140 to 160
        self.MainTemp_meter1.grid(row=1, column=3, padx=10, pady=10, rowspan=5)
        self.MainTemp_meter1.set(temp_reading_top)

        self.label3 = ctk.CTkLabel(self, text="Temperature")
        self.label3.grid(row=0, column=3, padx=10)

        #self.meter2 = Meter(self, radius=200, start=0, end=70, border_width=0, fg="#242424", bg="#242424",
               #text_color="white", start_angle=270, end_angle=-270,
               #text_font="DS-Digital 12", scale_color="white", needle_color="red", text=" \N{DEGREE SIGN}C")
        #self.meter2.set_mark(50, 70) # set red marking from 140 to 160
        #self.meter2.grid(row=6, column=3, padx=10, pady=10, rowspan=5)
        #self.meter2.set(temp_reading_bottom)
        
        self.label_t1 = ctk.CTkLabel(self, text="Timer 1: ")
        self.label_t1.grid(row=8, column=0, padx=10)

        self.progressbar_t1 = ctk.CTkProgressBar(self)
        self.progressbar_t1.grid(row=8, column=1, padx=(0, 10), pady=(10, 10), sticky="ew")

        self.label_t2 = ctk.CTkLabel(self, text="Timer 2: ")
        self.label_t2.grid(row=9, column=0, padx=10)

        self.progressbar_t2 = ctk.CTkProgressBar(self)
        self.progressbar_t2.grid(row=9, column=1, padx=(0, 10), pady=(10, 10), sticky="ew")

        self.label_t3 = ctk.CTkLabel(self, text="Timer 3: ")
        self.label_t3.grid(row=10, column=0, padx=10)

        self.progressbar_t3 = ctk.CTkProgressBar(self)
        self.progressbar_t3.grid(row=10, column=1, padx=(0, 10), pady=(10, 10), sticky="ew")
        
        self.img_sun = ctk.CTkImage(light_image=Image.open("sun.png"),
                                  dark_image=Image.open("sun.png"),
                                  size=(150, 150))
        self.label_sun = ctk.CTkLabel(self, text="", image=self.img_sun)
        self.label_sun.grid(row=2, column=0, padx=(10, 10), pady=(10, 10), sticky="ew")

        if temp_reading_top > 50:
            self.label2 = ctk.CTkLabel(self, text="warning: temperature exceeded")
            self.label2.grid(row=0, column=1, padx=20)
            self.img_cross = ctk.CTkImage(light_image=Image.open("cross.png"),
                                  dark_image=Image.open("cross.png"),
                                  size=(150, 150))
            self.label_cross = ctk.CTkLabel(self, text="", image=self.img_cross)
            self.label_cross.grid(row=1, column=1, padx=(20, 10), pady=(10, 10), sticky="ew")
        
        updater.update_readings(self)
python-3.x 用户界面 错误处理 传感器 pyupdater

评论


答: 暂无答案