Python只销毁tkinter子类小部件

时间:2018-05-23 16:30:04

标签: python python-3.x tkinter widget subclass

我用tkinter小部件编写了一个子类。在for-loop中,我将其中的一些放在一个Frame中。此框架还包含标签和条目。 现在我要销毁所有子类窗口小部件,但不要销毁Label和Entry。

我试过这样:

for child in self.frame.winfo_children():
    if child.winfo_class() == "???":
         [...]

但我无法弄清楚我必须使用什么,所以我会用???作为占位符。

我将它们放在使用此代码的规则中:

db.execute("SELECT * FROM UsedSystems")
rows = db.fetchall()
i = 0

for row in rows:
    image_path = activepath+rows[i][0]
    name = rows[i][1]
    performance = rows[i][2]
    project = rows[i][3]
    date = rows[i][4]              

    self.e10 = CustomWidget(self.frame, image_path, name, performance, project, date)
    self.e10.grid(row=1+i,column=0, columnspan=2)

    i+=1

1 个答案:

答案 0 :(得分:0)

尝试使用isinstance内置函数检查类(如下所示):

for child in self.frame.winfo_children():
    if not (isinstance (child, Label) or isinstance (child, Entry)):
        child.destroy ()

如果它们不是LabelEntry,则会销毁任何小部件。但是,它无法区分不同的Label窗口小部件(例如),并且会同时保留。