简体   繁体   中英

Inheriting methods from a Frame Class, in a toplevel widget Class

Currently, I need to access the methods and instance variables in a toplevel class from a ttk.Frame class; the program calculates certain values inside the ttk.Frame class, then, I want to be able to use some of the functions used to calculate those previous values from the ttk.Frame , in the toplevel .

class ReservoirDataFrame(ttk.Frame):
    def __init__(self, parent, controller):
        ttk.Frame.__init__(self, parent)
        self.controller = controller
        self.grid(row=0, column=0, sticky="nsew")
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)
        self.main()
        ...
    def main(self):
        pass
    def fluid_properties_calculate(self):
        pass

So, pretty much, what I want to be able to do is something like:

class CurveIPR(tk.Toplevel, ReservoirDataFrame):
    def __init__(self, controller):
        tk.Toplevel.__init__(self, controller)
        ReservoirDataFrame.__init__(self, controller)
        self.controller = controller
        self.reservoir_data_frame = ReservoirDataFrame(self)
        self.minsize(600, 480)
        self.title("Curva de oferta IPR (Inflow performance relationship)")
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)
        self.grab_set()
        self.main()

    def main(self):
        self.reservoir_data_frame.fluid_properties_calculate()

You need a bog-standard object, not inheritance.

class CurveIPR(tk.Toplevel):
    def __init__(self, controller):
        tk.Toplevel.__init__(self, controller)
        self.controller = controller
        self.reservoir_data_frame = ReservoirDataFrame(self, controller)
        # you really ought to grid it here, not in ReservoirDataFrame
        self.minsize(600, 480)
        self.title("Curva de oferta IPR (Inflow performance relationship)")
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)
        self.grab_set()
        self.main()

    def main(self):
        self.reservoir_data_frame.fluid_properties_calculate()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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