【发布时间】:2021-11-13 03:39:15
【问题描述】:
我有框架类 A 和 B, B 类是在 A 类中“创建”的, A 的父级是“self”。
问题:结果表明 B 在 A 之外。 没有出现错误。
在另一篇文章中回答说小部件返回无。 所以默认选择根窗口。
但 print(instance B) 返回 ".!instance B name" 而不是 None。
这是代码:
import tkinter as tk
# class B
class Frm_btn(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, width=24, height=24, bg="green")
self.parent=parent
# class A
class Frm_tool(tk.Frame):
def __init__(self):
tk.Frame.__init__(self, width=48, height=48, bg="blue")
self.parent=win
# instance of class B
self.frm_btn=Frm_btn(self)
self.frm_btn.pack()
print(self)# return ".!frm_tool"
print(self.frm_btn)# return ".!frm_btn"
win=tk.Tk()
win.configure(bg="red")
# instance of class A
frm_tool=Frm_tool()
frm_tool.pack()
win.mainloop()
【问题讨论】:
-
不能只设置
self.parent,需要在tk.Frame.__init__调用后添加self -
谢谢您的回答
标签: python class tkinter instance parent