单击按钮后 Python Tkinter 销毁标签 [英] Python Tkinter destroy label after the click of a button

查看:46
本文介绍了单击按钮后 Python Tkinter 销毁标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在单击按钮后出现的标签,但是在每次单击按钮后,前一个标签保持在其位置并创建一个新标签,我希望将新标签放置在旧的,代码如下:

I have a label which appears after the click of a button, however after each click of the button the previous label remains in its position and a new one is created, I would like for the new label to be put in place of the old one, the code is below:

browser = webdriver.PhantomJS()
browser.get('http://www.ipvoid.com/ip-blacklist-check/')

def helloCallBack():
   ip = entry.get()
   elem = browser.find_element_by_name('ip')  # Find the search box
   elem.send_keys(ip + Keys.RETURN)
   for elem in itertools.chain(browser.find_elements_by_xpath('.//span[@class = "label label-success"]'), browser.find_elements_by_xpath('.//span[@class = "label label-danger"]')):
      print elem.text
   label2 = tk.Label(root1, text="IPVoid: " + elem.text)
   label2.pack(side=tk.BOTTOM)

root1 = tk.Tk()
root1.geometry("500x500")
root1.title("CZEKER")
label = tk.Label(root1, text='Input IP Address:') 
entry = tk.Entry(root1)
label.pack(side=tk.TOP)
entry.pack()
B = tk.Button(root1, text = "Analiza", command = helloCallBack)
B.pack()
label2 = tk.Label(root1, text="IPVoid: " + elem.text)
label2.pack(side=tk.BOTTOM)

root1.mainloop()

我想我不太确定这个循环是如何工作的,只要单击按钮 B,程序就会执行方法 helloCallBack(),其中填充了 label2.但是我无法在方法开始时销毁标签,因为它还没有被填充,但我也不能在之后销毁它,因为它会在它出现后立即销毁它,而不是点击按钮 B 的那一刻第二次.我的问题如下:每次单击按钮 B 后,如何让新标签 2 代替旧标签 2.谢谢.

I think i'm not quite sure how this loop works, whenever button B is clicked, the program executes method helloCallBack(), in which label2 is populated. However i cannot destroy the label at the beginning of the method since it hasn't been populated yet, but i can't destroy it after either, because it would immediately destroy it after it appears, not the moment button B would be clicked a second time. My question is as follows: How can i make a new label2 appear in place of the old label2 after each click of button B. Thank you.

推荐答案

与其每次点击都重新创建该标签,您可以使用空文本创建它,然后在每次点击时更改其内容.

Instead of recreating that label every click, you can create it with empty text then change its content on every click.

def helloCallBack():
    ...
    ...
    label2.configure(text="IPVoid: " + elem.text)

label2 = tk.Label(root1, text="") 
#or just label2 = tk.Label(root1) used that one to make it explicit right now
label2.pack(side=tk.BOTTOM)

这篇关于单击按钮后 Python Tkinter 销毁标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆