从另一个python类导入数据

从另一个python类导入数据,python,class,inheritance,tkinter,parent,Python,Class,Inheritance,Tkinter,Parent,我需要从Monitor类访问温度变量并在Graph类上打印。我该怎么做?请参阅下面应该编译的代码 from tkinter import * import tkinter as tk import time class ScientificPumpGui(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs) self.container =

我需要从Monitor类访问温度变量并在Graph类上打印。我该怎么做?请参阅下面应该编译的代码

from tkinter import *
import tkinter as tk
import time

class ScientificPumpGui(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        self.container = tk.Frame(self)
        self.container.pack(side="top", fill="both", expand=True)
        self.container.grid_rowconfigure(0, weight=1)
        self.container.grid_columnconfigure(0, weight=1)
        self.frames = {}

        for F in (MonitorPage, GraphPage):
            frame = F(self.container)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
            self.show_frame(MonitorPage)

        self.create_buttons()
    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()
    def exit_app(self):
        exit()
    def create_buttons(self):
        main_b_height = 2
        main_b_width = 20
        page_button_pady = 10
        self.page_button_main_toolbar = tk.Frame(self, borderwidth=1)
        self.page_button_main_toolbar.pack(side=TOP, anchor=CENTER, fill=X)
        self.page_button_toolbar = tk.Frame(self.page_button_main_toolbar, borderwidth=1)
        self.page_button_toolbar.pack(side=TOP, anchor=CENTER)

        self.monitor_page_button = Button(self.page_button_toolbar, text="Monitor Page", width=main_b_width, height=main_b_height, command=lambda: self.show_frame(MonitorPage))
        self.monitor_page_button.pack(side=LEFT, anchor=CENTER, pady=page_button_pady)
        self.graph_page_button = Button(self.page_button_toolbar, text="Graph Page", width=main_b_width, height=main_b_height, command=lambda: self.show_frame(GraphPage))
        self.graph_page_button.pack(side=LEFT, anchor=CENTER, pady=page_button_pady)
        self.exit_app_button = Button(self.page_button_toolbar, text="Exit App", width=main_b_width, height=main_b_height, command=lambda: ScientificPumpGui.exit_app(0))
        self.exit_app_button.pack(side=LEFT, anchor=CENTER, pady=page_button_pady)


class MonitorPage(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.monitor_data_counter = 0

        self.page_label = tk.Label(self, text="Monitor Page")
        self.page_label.pack(pady=10, padx=10)
    def value_function(self):
        self.temperature = 100

class GraphPage(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        label = tk.Label(self, text="Graph Page!")
        label.pack(pady=5, padx=10)

app = ScientificPumpGui()
app.mainloop()
当我尝试使用以下工具读取温度时:

monitor_page=MonitorPage(ScientificPumpGui())
print(str(monitor_page.temperature))
monitor_page.mainloop()
我得到的错误是:

AttributeError:'MonitorPage'对象没有属性'temperature'


之所以出现此错误,是因为成员
temperature
value\u函数
方法中进行了初始化,而该方法未被调用

由于您没有调用此方法,因此未初始化成员
temperature
,因此您会得到错误

为了防止此错误,您应该在
\uuuu init\uuu
方法中使用默认值定义成员
温度


您还可以通过调用
value\u函数
方法来修复它,以便初始化成员
temperature
您的
MonitorPage
类不在构造函数中声明
temperature
,而是在
value\u函数中声明


您可以在
函数中声明
温度
,也可以在阅读
温度
之前调用
函数

,谢谢。现在错误消失了。如何从graph类有效访问温度?