如何从另一个 class´ 实例获取属性?
How to get an attribute from another class´ instance?
在编写 tkinter 应用程序时,我在 Python 中挣扎了一个星期。我已经简化了应用程序以仅显示问题。我有两个框架,每个框架都有两个按钮 - 第一行的按钮增加了框架的数量(self.top_frame_number
或 self.bottom_frame_number
,第二行的按钮应该让我从另一个中得到一个数字框架(所以顶部框架中的按钮应该让我从底部框架中获取数字)。
我不知道如何完成这个,这意味着我不知道如何从另一个 class´ 实例访问一个 class´ 实例属性,而它们都是主应用程序的属性 class。
我一直在搜索问题,但没有找到类似的例子,而另一个问题的答案并没有帮助我解决问题。这是完整的代码:
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,但没有成功。所以我尝试了:
- 继承 - 它以某种方式起作用,但我不想使用继承,因为
TopFrame
和 BottomFrame
class 是平等的,而不是父子关系
- 合成 - 我无法用合成修复它(=在
__init__
中初始化另一个 class,因为它会导致无限递归错误。
我应该如何正确访问其他 class´ 实例中的号码?
jasonharper 为我指出了正确的方向。我添加了对主应用程序 class 的引用并且它有效。所以整个代码是:
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 classes 引用应用程序 class 而无需在应用程序的 __init__
中指定 self
(这意味着没有 self.top_frame = TopFrame(self.master, self)
和 self.bottom_frame = BottomFrame(self.master, self)
) 以及 TopFrame 和 BottomFrame 中的相应参考 classes (self.master = master
),我很开放...
在编写 tkinter 应用程序时,我在 Python 中挣扎了一个星期。我已经简化了应用程序以仅显示问题。我有两个框架,每个框架都有两个按钮 - 第一行的按钮增加了框架的数量(self.top_frame_number
或 self.bottom_frame_number
,第二行的按钮应该让我从另一个中得到一个数字框架(所以顶部框架中的按钮应该让我从底部框架中获取数字)。
我不知道如何完成这个,这意味着我不知道如何从另一个 class´ 实例访问一个 class´ 实例属性,而它们都是主应用程序的属性 class。
我一直在搜索问题,但没有找到类似的例子,而另一个问题的答案并没有帮助我解决问题。这是完整的代码:
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,但没有成功。所以我尝试了:
- 继承 - 它以某种方式起作用,但我不想使用继承,因为
TopFrame
和BottomFrame
class 是平等的,而不是父子关系 - 合成 - 我无法用合成修复它(=在
__init__
中初始化另一个 class,因为它会导致无限递归错误。
我应该如何正确访问其他 class´ 实例中的号码?
jasonharper 为我指出了正确的方向。我添加了对主应用程序 class 的引用并且它有效。所以整个代码是:
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 classes 引用应用程序 class 而无需在应用程序的 __init__
中指定 self
(这意味着没有 self.top_frame = TopFrame(self.master, self)
和 self.bottom_frame = BottomFrame(self.master, self)
) 以及 TopFrame 和 BottomFrame 中的相应参考 classes (self.master = master
),我很开放...