[英]How do I delete all old labels using a button?
我想創建它,以便我可以更輕松地檢查加密貨幣的價格。 我想存儲它目前所做的歷史記錄。 但我也想要一種刪除所有舊標簽的方法。 我只能刪除最近的一個,因為我每次點擊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()
解決方案是將標簽存儲在一個列表中,以便您以后可以對其進行迭代,或者您可以將它們放在一個框架中,然后遍歷該框架的所有子級,對每個子級調用destroy
。 或者,只保留代碼原樣並循環遍歷root
的所有孩子,並且只銷毀標簽。
另一個解決方案是使用單個Text
或Listbox
小部件來存儲所有項目,而不是使用單獨Label
小部件。 這兩個小部件都可以很容易地刪除它們的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.