如果小部件来自不同 类,如何将它们插入另一个小部件。以及如何创建关闭子窗口小部件的通用功能?
how to insert a widget to another, if they are from different classes. and how to create a universal function of closing the child widgets?
这是我的代码。我不能将 label2 放到 self.main,而且我不知道如何编写一个通用函数代码,它会关闭可以在参数中指定的子窗口小部件。
import tkinter
class mainwin:
def __init__(self):
self.root = tkinter.Tk()
self.main = tkinter.Canvas(self.root, width=200, height=400)
self.main.place(x=0, y=0, relwidth=1, relheight=1)
self.main.config(bg='green')
self.root.mainloop()
class addlabel:
def __init__(self):
self.label2 = tkinter.Label(mainwin.main, height=2, width=50, text='Hello Noob!!')
#can't put on the canvas 'main'
self.label2.place(x=0, y=50)
self.exit_button = tkinter.Button(self.label2, text='Exit')
self.exit.button.bind('<1>', quit_from_widget)
'''
class quit_from_widget:
def __init__(self):
# what code should be written here, to quit any child widget.
'''
mainwin()
addlabel()
您也许可以使用:
mylist = parent.winfo_children();
然后使用 for 循环和 destroy() 关闭它们
无法放入标签的主要原因是您在调用 addLabel 之前调用了 mainloop()。程序循环执行代码,直到您关闭 mainwin() 函数后才执行 addlabel()。
其次,你做不到mainw.main。 class 没有引用该函数。而是尝试像这样向您的 addlabel 添加父函数:
class addlabel:
def __init__(self, parent):
self.label2 = tkinter.Label(parent, height=2, width=50, text='Hello Noob!!')
self.label2.place(x=0, y=50)
self.exit_button = tkinter.Button(self.label2, text='Exit')
self.exit_button.bind('<1>', quit)
然后,当您调用 mainw class 中的函数时(在 self.root.mainloop() 行之前),您将编写:
addlabel(self.main)
import tkinter
class mainwin:
def __init__(self):
self.root = tkinter.Tk()
self.main = tkinter.Canvas(self.root, width=200, height=400)
self.main.place(x=0, y=0, relwidth=1, relheight=1)
self.main.config(bg='green')
self.root.mainloop()
class CustomLabel(tkinter.Frame):
def __init__(self, parent):
tkinter.Frame.__init__(self, parent)
self.label = tkinter.Label(self, height=20, width=30, bg='Red', fg='white', text='Hello')
self.exit_button = tkinter.Button(self, command=self.destroy)
# pack these widgets into this frame. You can use grid or
# place, but pack is easiest for such a simple layout
self.exit_button.pack(side="right")
self.label.pack(side="left", fill="both", expand=True)
window = mainwin()
label = CustomLabel(window.main)
没有任何反应。给出绿色背景并且子部件不可见。但是关闭时,他写了一个错误:
....
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: 无法调用 "frame" 命令:应用程序已被销毁
进程已完成,退出代码为 1
这是我的代码。我不能将 label2 放到 self.main,而且我不知道如何编写一个通用函数代码,它会关闭可以在参数中指定的子窗口小部件。
import tkinter
class mainwin:
def __init__(self):
self.root = tkinter.Tk()
self.main = tkinter.Canvas(self.root, width=200, height=400)
self.main.place(x=0, y=0, relwidth=1, relheight=1)
self.main.config(bg='green')
self.root.mainloop()
class addlabel:
def __init__(self):
self.label2 = tkinter.Label(mainwin.main, height=2, width=50, text='Hello Noob!!')
#can't put on the canvas 'main'
self.label2.place(x=0, y=50)
self.exit_button = tkinter.Button(self.label2, text='Exit')
self.exit.button.bind('<1>', quit_from_widget)
'''
class quit_from_widget:
def __init__(self):
# what code should be written here, to quit any child widget.
'''
mainwin()
addlabel()
您也许可以使用:
mylist = parent.winfo_children();
然后使用 for 循环和 destroy() 关闭它们
无法放入标签的主要原因是您在调用 addLabel 之前调用了 mainloop()。程序循环执行代码,直到您关闭 mainwin() 函数后才执行 addlabel()。
其次,你做不到mainw.main。 class 没有引用该函数。而是尝试像这样向您的 addlabel 添加父函数:
class addlabel:
def __init__(self, parent):
self.label2 = tkinter.Label(parent, height=2, width=50, text='Hello Noob!!')
self.label2.place(x=0, y=50)
self.exit_button = tkinter.Button(self.label2, text='Exit')
self.exit_button.bind('<1>', quit)
然后,当您调用 mainw class 中的函数时(在 self.root.mainloop() 行之前),您将编写:
addlabel(self.main)
import tkinter
class mainwin:
def __init__(self):
self.root = tkinter.Tk()
self.main = tkinter.Canvas(self.root, width=200, height=400)
self.main.place(x=0, y=0, relwidth=1, relheight=1)
self.main.config(bg='green')
self.root.mainloop()
class CustomLabel(tkinter.Frame):
def __init__(self, parent):
tkinter.Frame.__init__(self, parent)
self.label = tkinter.Label(self, height=20, width=30, bg='Red', fg='white', text='Hello')
self.exit_button = tkinter.Button(self, command=self.destroy)
# pack these widgets into this frame. You can use grid or
# place, but pack is easiest for such a simple layout
self.exit_button.pack(side="right")
self.label.pack(side="left", fill="both", expand=True)
window = mainwin()
label = CustomLabel(window.main)
没有任何反应。给出绿色背景并且子部件不可见。但是关闭时,他写了一个错误:
....
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: 无法调用 "frame" 命令:应用程序已被销毁
进程已完成,退出代码为 1