显示当前标签消失前一个 [英] display the current label disappearing the previous
本文介绍了显示当前标签消失前一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试一个程序,它将显示在输入框中输入的相应名称的标签.问题:它重叠并显示标签,而不是消失之前的条目标签.请帮我消失之前的条目并显示标签.编码:
I am trying a program,which will display label for the respective name entered in the Entry box. Problem: It overlaps and displays the label,instead of disappearing the previous entry label. Please help me to disappear the previous entry and display the label. Coding:
import Tkinter as tki
class App(object):
def __init__(self,root):
self.root = root
self.txt_frm = tki.Frame(self.root, width=900, height=900)
self.txt_frm.pack(fill="both", expand=True)
button3 = tki.Button(self.txt_frm,text="CLICK 1", command = self.retrieve_inpu)
button3.grid(column=0,row=2)
self.entry = tki.Entry(self.txt_frm)
self.entry.grid(column=1,row=0)
def retrieve_inpu(self):
entr = self.entry.get()
label = tki.Label(self.txt_frm,text=entr)
label.grid(column=0,row=3)
root = tki.Tk()
app = App(root)
root.mainloop()
推荐答案
您需要保留对标签的引用,并在创建之前对其调用 .destroy()
以摆脱它新标签.
You need to keep a reference to the label, and call .destroy()
on it to get rid of it before you create the new Label.
更好的是,如果需要的话,您可以更改文本.尝试使用以下代码代替您自己的代码:
Even better, you could just change the text if that is all the needs to happen. Try the following code in place of your own:
import Tkinter as tki
class App(object):
def __init__(self,root):
self.root = root
self.txt_frm = tki.Frame(self.root, width=900, height=900)
self.txt_frm.pack(fill="both", expand=True)
button3 = tki.Button(self.txt_frm,text="CLICK 1", command = self.retrieve_inpu)
button3.grid(column=0,row=2)
self.entry = tki.Entry(self.txt_frm)
self.entry.grid(column=1,row=0)
self.label = tki.Label(self.txt_frm)
self.label.grid(column=0,row=3)
def retrieve_inpu(self):
entr = self.entry.get()
self.label.config(text=entr)
root = tki.Tk()
app = App(root)
root.mainloop()
这篇关于显示当前标签消失前一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文