如何从另一个类的实例获取属性?

时间:2018-12-27 13:30:37

标签: python-3.x class tkinter instance-variables

在编写tkinter应用程序时,我一直用Python努力工作了一个星期。我已经简化了应用程序以仅显示问题。我有两个框架,每个框架都有两个按钮-第一行中的按钮增加了框架的编号(self.top_frame_numberself.bottom_frame_number,第二行中的按钮应该为我提供另一个编号框架(因此顶部框架中的按钮应该让我从底部框架中获得数字)。

我不知道如何完成此操作,这意味着我不知道如何从另一个类的实例访问类的实例属性,而这两个都是主Application类的属性。

我一直在搜索问题,但是没有找到类似的例子,而其他问题的答案也没有帮助我解决问题。这是完整的代码:

import tkinter as tk

class TopFrame:
    def __init__(self, parent):
        self.parent = parent
        self.topframe = tk.LabelFrame(self.parent, text="Top frame", width=300, height=100, bd=2)
        self.topframe.pack(side="top", fill="both")

        self.top_frame_number = 100

        # FIRST ROW WIDGETS
        self.topframe_tfn_label = tk.Label(self.topframe, text="number: ")
        self.topframe_tfn_label.grid(row=0, column=0, sticky="w")

        self.topframe_tfn = tk.Label(self.topframe)
        self.topframe_tfn.grid(row=0, column=1)
        self.topframe_tfn.configure(text=self.top_frame_number)

        self.topframe_tfn_button = tk.Button(self.topframe,
                                         text="increase",
                                         command=lambda: self.topframe_increase_top_frame_number())
        self.topframe_tfn_button.grid(row=0, column=2, pady=2, sticky="w")

        # SECOND ROW WIDGETS
        self.topframe_bfn_label = tk.Label(self.topframe, text="bottom frame number: ")
        self.topframe_bfn_label.grid(row=1, column=0, sticky="w")

        self.topframe_bfn = tk.Label(self.topframe)
        self.topframe_bfn.grid(row=1, column=1)
        self.topframe_bfn.configure(text="?")

        self.topframe_bfn_button = tk.Button(self.topframe,
                                         text="get bottom frame number",
                                         command=lambda: self.topframe_get_bottom_frame_number())
        self.topframe_bfn_button.grid(row=1, column=2, pady=2, sticky="e")

    def topframe_increase_top_frame_number(self):
        self.top_frame_number += 1
        self.topframe_tfn.configure(text=self.top_frame_number)

    def topframe_get_bottom_frame_number(self):
        pass
        # I AM STUCK HERE - How to get to the top frame number?

class BottomFrame:
    def __init__(self, parent):
        self.parent = parent
        self.bottomframe = tk.LabelFrame(self.parent, text="Bottom frame", width=300, height=100, bd=2)
        self.bottomframe.pack(side="top", fill="both")

        self.bottom_frame_number = 200

        # FIRST ROW
        self.bottomframe_tfn_label = tk.Label(self.bottomframe, text="number: ")
        self.bottomframe_tfn_label.grid(row=0, column=0, sticky="w")

        self.bottomframe_tfn = tk.Label(self.bottomframe)
        self.bottomframe_tfn.grid(row=0, column=1)
        self.bottomframe_tfn.configure(text=self.bottom_frame_number)

        self.bottomframe_tfn_button = tk.Button(self.bottomframe,
                                            text="increase",
                                            command=lambda: self.bottomframe_increase_bottom_frame_number())
        self.bottomframe_tfn_button.grid(row=0, column=2, pady=2, sticky="e")

        # SECOND ROW
        self.bottomframe_bfn_label = tk.Label(self.bottomframe, text="top frame number: ")
        self.bottomframe_bfn_label.grid(row=1, column=0, sticky="w")

        self.bottomframe_bfn = tk.Label(self.bottomframe)
        self.bottomframe_bfn.grid(row=1, column=1)
        self.bottomframe_bfn.configure(text="?")

        self.bottomframe_bfn_button = tk.Button(self.bottomframe,
                                            text="get top frame number",
                                            command=lambda: self.bottomframe_get_top_frame_number())
        self.bottomframe_bfn_button.grid(row=1, column=2, pady=2, sticky="e")

    def bottomframe_increase_bottom_frame_number(self):
        self.bottom_frame_number += 1
        self.bottomframe_tfn.configure(text=self.bottom_frame_number)

    def bottomframe_get_top_frame_number(self):
        pass
        # I AM STUCK HERE - How to get to the top frame number?

class Application:
    def __init__(self, master):
        self.master = master
        self.master.title("Show me numbers!")

        # -- FRAMES --
        self.top_frame = TopFrame(self.master)
        self.bottom_frame = BottomFrame(self.master)

if __name__ == "__main__":
    root = tk.Tk()

    Application(root)
    root.mainloop()

我一直在尝试从网络上的不同材料中学习oop,但没有成功。所以我尝试了:

  1. 继承-以某种方式起作用,但是我不想使用继承,因为TopFrameBottomFrame类是相等的,而不是父子类
  2. 组合-我无法使用组合来修复它(= __init__中初始化另一个类,因为它会导致无限递归错误。

我应该如何正确访问其他类实例中的号码?

1 个答案:

答案 0 :(得分:0)

jasonharper为我指明了正确的方向。我将引用添加到主Application类,并且它可以工作。所以整个代码是:

import tkinter as tk

class TopFrame:
    def __init__(self, parent, master):
        self.parent = parent
        self.master = master
        self.topframe = tk.LabelFrame(self.parent, text="Top frame", width=300, height=100, bd=2)
        self.topframe.pack(side="top", fill="both")

        self.top_frame_number = 100

        # FIRST ROW
        self.topframe_tfn_label = tk.Label(self.topframe, text="number: ")
        self.topframe_tfn_label.grid(row=0, column=0, sticky="w")

        self.topframe_tfn = tk.Label(self.topframe)
        self.topframe_tfn.grid(row=0, column=1)
        self.topframe_tfn.configure(text=self.top_frame_number)

        self.topframe_tfn_button = tk.Button(self.topframe,
                                         text="increase",
                                         command=lambda: self.topframe_increase_top_frame_number())
        self.topframe_tfn_button.grid(row=0, column=2, pady=2, sticky="w")

        # SECOND ROW
        self.topframe_bfn_label = tk.Label(self.topframe, text="bottom frame number: ")
        self.topframe_bfn_label.grid(row=1, column=0, sticky="w")

        self.topframe_bfn = tk.Label(self.topframe)
        self.topframe_bfn.grid(row=1, column=1)
        self.topframe_bfn.configure(text="?")

        self.topframe_bfn_button = tk.Button(self.topframe,
                                         text="get bottom frame number",
                                         command=lambda: self.topframe_get_bottom_frame_number())
        self.topframe_bfn_button.grid(row=1, column=2, pady=2, sticky="e")

    def topframe_increase_top_frame_number(self):
        self.top_frame_number += 1
        self.topframe_tfn.configure(text=self.top_frame_number)

    def topframe_get_bottom_frame_number(self):
        self.topframe_bfn.configure(text=self.master.bottom_frame.bottom_frame_number)


class BottomFrame:
    def __init__(self, parent, master):
        self.parent = parent
        self.master = master
        self.bottomframe = tk.LabelFrame(self.parent, text="Bottom frame", width=300, height=100, bd=2)
        self.bottomframe.pack(side="top", fill="both")

        self.bottom_frame_number = 200

        # FIRST ROW
        self.bottomframe_bfn_label = tk.Label(self.bottomframe, text="number: ")
        self.bottomframe_bfn_label.grid(row=0, column=0, sticky="w")

        self.bottomframe_bfn = tk.Label(self.bottomframe)
        self.bottomframe_bfn.grid(row=0, column=1)
        self.bottomframe_bfn.configure(text=self.bottom_frame_number)

        self.bottomframe_bfn_button = tk.Button(self.bottomframe,
                                            text="increase",
                                            command=lambda: self.bottomframe_increase_bottom_frame_number())
        self.bottomframe_bfn_button.grid(row=0, column=2, pady=2, sticky="e")

        # SECOND ROW
        self.bottomframe_tfn_label = tk.Label(self.bottomframe, text="top frame number: ")
        self.bottomframe_tfn_label.grid(row=1, column=0, sticky="w")

        self.bottomframe_tfn = tk.Label(self.bottomframe)
        self.bottomframe_tfn.grid(row=1, column=1)
        self.bottomframe_tfn.configure(text="?")

        self.bottomframe_tfn_button = tk.Button(self.bottomframe,
                                            text="get top frame number",
                                            command=lambda: self.bottomframe_get_top_frame_number())
        self.bottomframe_tfn_button.grid(row=1, column=2, pady=2, sticky="e")

    def bottomframe_increase_bottom_frame_number(self):
        self.bottom_frame_number += 1
        self.bottomframe_bfn.configure(text=self.bottom_frame_number)

    def bottomframe_get_top_frame_number(self):
        self.bottomframe_tfn.configure(text=self.master.top_frame.top_frame_number)

class Application:
    def __init__(self, master):
        self.master = master
        self.master.title("Show me numbers!")

        # -- FRAMES --
        self.top_frame = TopFrame(self.master, self)
        self.bottom_frame = BottomFrame(self.master, self)

if __name__ == "__main__":
    root = tk.Tk()

    Application(root)
    root.mainloop()

如果有更好的方法可以从TopFrame或BottomFrame类中引用Application类,而无需在Application的self中指定__init__(这意味着没有self.top_frame = TopFrame(self.master, self)和{{1} })和TopFrame和BottomFrame类(self.bottom_frame = BottomFrame(self.master, self))中的相应引用,我很开放...