简体   繁体   English

Python仅销毁tkinter子类小部件

[英]Python destroy only tkinter subclass widget

I wrote a subclass with tkinter widgets. 我用tkinter小部件编写了一个子类。 In a for-loop i place few of them in a Frame. 在for循环中,我很少将它们放在框架中。 This Frame also contains a Label and a Entry. 该框架还包含一个标签和一个条目。 Now i want to destroy all of my subclass widgets but NOT the Label and the Entry. 现在,我想销毁所有子类小部件,但不销毁Label和Entry。

I tried it like this: 我这样尝试过:

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

But I wasnt able to figure out what i have to use, so i will use ??? 但是我无法弄清楚我必须使用什么,所以我将使用??? as a placeholder for this. 作为此的占位符。

I place them in a rule with this Code: 我使用以下代码将它们放在规则中:

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

Try using the isinstance built-in function to check the class (as shown below): 尝试使用isinstance内置函数检查类(如下所示):

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

This will destroy any widget if they are not a Label and Entry . 如果它们不是Label and Entry则会破坏任何小部件。 However, it cannot distinguish between different Label widgets (for example) and will leave BOTH. 但是,它无法区分不同的Label窗口小部件(例如),并且将全部保留。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM