如何使用按钮删除所有旧标签?

时间:2021-04-21 18:17:40

标签: python tkinter

我想创建它,以便我可以更轻松地检查加密货币的价格。 我想存储它目前所做的历史记录。但我也想要一种删除所有旧标签的方法。 每次点击 CHECK BOG 按钮时,我都会为 price_label 重新分配一个新值,因此我只能删除最近的一个。

import tkinter as tk
import webbrowser
from tkinter import *
import re
from urllib.request import urlopen
root = Tk()


url = "https://coinmarketcap.com/currencies/bogged-finance/"

def check():
    url = "https://coinmarketcap.com/currencies/bogged-finance/"
    page = urlopen(url)
    html = page.read().decode("utf-8")
    pattern = '<div class="priceValue___11gHJ">.*?</div>'
    match_results = re.search(pattern, html, re.IGNORECASE)
    title = match_results.group()
    title = re.sub("<.*?>", "", title) # Remove HTML tags
    global price_label
    price_label = Label(root, text = title)
    price_label.pack()




#trying to clear 
def clear():
    print("hello")
    price_label.pack_forget()


#trying to open link

def open():
    webbrowser.open('https://coinmarketcap.com/currencies/bogged-finance/')



button_check = Button(root, text  ="CHECK BOG", command = check)

button_clear = Button(root, text ="CLEAR", command = clear)

button_open = Button(root, text = 'Website', command = open)

button_clear.pack()
button_check.pack()
button_open.pack()


root.mainloop()

1 个答案:

答案 0 :(得分:0)

解决方案是要么将标签存储在一个列表中,以便您以后可以对其进行迭代,或者您可以将它们放在一个框架中,然后循环遍历该框架的所有子项,调用 destroy on每个孩子。或者,保留代码原样并循环遍历 root 的所有子项,并且只销毁标签。

另一种解决方案是使用单个 TextListbox 小部件来存储所有项目,而不是使用单个 Label 小部件。这两个小部件都可以轻松删除其内容。

相关问题