简体   繁体   English

Widget.destroy() 在 tkinter python 中不起作用

[英]Widget.destroy() doesn't work in tkinter python

While users sign up I'm making sure that no required entry fields are empty ie no entry widgets are empty.当用户注册时,我确保没有必填字段为空,即没有任何条目小部件为空。 I put a label l1 on the window when empty entry fields are given.当给出空输入字段时,我在窗口上放置了一个标签 l1。 Now when the user clicks the button again after entering all the information I want to remove this label from the window.现在,当用户在输入所有信息后再次单击按钮时,我想从窗口中删除此标签。 I've written the following code.我编写了以下代码。

                l1_info = {}
                def sign_up_now():

                    global l1_info

                    l1 = Label(myFrame5, text="*Required field(s)\nempty.", font=("Helvetica 18 bold", 13),
                               padx=20, pady=4, bd=1, relief="groove", fg="#FF0000")

                    if user_password.index("end") == 0 or user_first_name.index("end") == 0 or user_last_name.index\
                            ("end") == 0:
                        l1.grid(row=8, column=0, columnspan=2, pady=5, padx=3, sticky="W")
                        l1_info = l1.grid_info()
                    else:
                        if l1_info != {}:
                            if l1_info["row"] == 8:
                                l1.destroy()
                            else:
                                print("Not present.")

l1_info is a global variable and its previous value remains every time the function is called. l1_info 是一个全局变量,每次调用函数时它的先前值都会保留。 Although I've checked that the condition in the if condition becomes true (l1_info["row"] == 8), l1 label still isn't destroyed.虽然我已经检查了 if 条件中的条件是否为真(l1_info["row"] == 8),但 l1 标签仍然没有被破坏。 Why is it not destroying?为什么不破坏?

Why is it not destroying?为什么不破坏?

Every time you call sign_up_now you're creating a new label.每次调用sign_up_now时,您都在创建一个新标签。 So, the first time through, if one of the fields is empty you create a label.因此,第一次通过时,如果其中一个字段为空,您将创建一个标签。 Unless you go through the logic that causes the label to be destroyed, the label won't be destroyed.除非您通过导致标签被销毁的逻辑,否则标签不会被销毁。 At this point you have one label.此时您有一个标签。

Now, the user enters some data and clicks the button.现在,用户输入一些数据并单击按钮。 The first thing you do is create another label.您要做的第一件事是创建另一个标签。 If they entered all the data, this second label is destroyed but the first one continues to exist.如果他们输入了所有数据,则第二个标签将被销毁,但第一个标签将继续存在。 If they don't fulfill all of the criteria, you now have two labels.如果它们不满足所有条件,您现在有两个标签。 When the user clicks on the button again, you create a label and now have three.当用户再次单击按钮时,您创建了一个标签,现在有三个。 And so on.等等。

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

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