[英]Why destroy() doesn't work after 'label_x' is modified?
If I click on button ' xxx ' after starting this little program, it destroys all widgets of the window except button ' greet me '... as it should... But if I write something into the yellow the entry field then click on ' greet me ' and after that on ' xxx '... then for some reason the modified 'label_x' will not be deleted anymore... Also if I write multiple times some names in the entry box then click ' greet me ' it is only writing on the previous label insted of destroying/deleting it first.如果我在启动这个小程序后点击按钮“ xxx ”,它会破坏窗口的所有小部件,除了按钮“问候我”......它应该......但是如果我在黄色的输入字段中写入一些东西然后点击'问候我'然后在' xxx '上......然后由于某种原因修改后的'label_x'将不再被删除......另外如果我在输入框中多次写一些名字然后点击'问候我'它只写在之前的标签上而不是先销毁/删除它。 Why is that and how could it be solved?
为什么会这样,如何解决?
from tkinter import *
root = Tk()
# Creating an entry box
entry_box = Entry(root, width=10, relief="solid", bg="yellow" )
entry_box.grid(column=0, row=0)
entry_box.insert(3, "type name")
# Creating label widgets
label_x = Label(root, text="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
label_x.grid(column=0, row=2)
label_minus = Label(root, text="------------------------------")
label_minus.grid(column=0, row=3)
def greeter():
label_x = Label(root, text="Welcome " + entry_box.get())
label_x.grid(column=0, row=2)
# Creating greeter button
myButton = Button(root, text="greet me", command = lambda: [label_x.destroy(), greeter()])
myButton.grid(column=0, row=1)
x = Button(root, text="xxx", command = lambda: [label_x.destroy(), entry_box.destroy(), label_minus.destroy()])
x.grid(column=0, row=4)
root.mainloop()
You're creating a new instance of label_x every time you're calling the greeter() function.每次调用 greeter() 函数时,都会创建一个新的 label_x 实例。 A label_x which is different from the global label_x variable.
不同于全局 label_x 变量的 label_x。
A quick fix is to declare label_x as a global variable inside the greeter() function.一个快速修复方法是在 greeter() 函数中将 label_x 声明为全局变量。 Like this:
像这样:
def greeter():
global label_x
label_x = Label(root, text="Welcome " + entry_box.get())
label_x.grid(column=0, row=2)
It might not be the best solution but it should work.它可能不是最好的解决方案,但它应该有效。
I solved your problem.我解决了你的问题。 You do not duplicate
label_x
and label_x.grid
in greeter function.您不要在欢迎函数中复制
label_x
和label_x.grid
。 Just add label_x.configure
, and using f-String format in greeter function.只需添加
label_x.configure
,并在 greeter 函数中使用 f-String 格式。 Also you are missing myButton.destroy()
in mybutton
command.此外,您在
mybutton
命令中缺少myButton.destroy()
。
here is code:这是代码:
from tkinter import *
root = Tk()
# Creating an entry box
entry_box = Entry(root, width=10, relief="solid", bg="yellow" )
entry_box.grid(column=0, row=0)
entry_box.insert(3, "type name")
# Creating label widgets
label_x = Label(root, text="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
label_x.grid(column=0, row=2)
label_minus = Label(root, text="------------------------------")
label_minus.grid(column=0, row=3)
def greeter():
label_x.configure(text=f"Welcome { entry_box.get()}")
# Creating greeter button
myButton = Button(root, text="greet me", command = greeter)
myButton.grid(column=0, row=1)
x = Button(root, text="xxx",
command = lambda: [myButton.destroy(), label_x.destroy(),
entry_box.destroy(), label_minus.destroy()])
x.grid(column=0, row=4)
root.mainloop()
Result before;结果之前; Result enter name and Result destroy widgets:
结果输入名称和结果销毁小部件:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.