从另一个类python导入数据

时间:2017-06-12 12:56:52

标签: python class inheritance tkinter parent

我需要从Monitor Class访问温度变量并在Graph Class上打印。我怎样才能做到这一点?请参阅下面的代码,应编译。

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'

2 个答案:

答案 0 :(得分:2)

您收到此错误是因为成员temperature已在value_function方法中初始化,但未被调用。

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

为了防止出现此错误,您应该使用默认值在temperature方法中定义成员__init__

您也可以通过调用value_function方法来初始化成员temperature来解决此问题。

答案 1 :(得分:2)

您的MonitorPage课程未在构造函数中声明temperature,而是在value_function中声明。

您可以在temperature函数内声明__init__,也可以在阅读value_function之前致电temperature