简体   繁体   English

实时更新 tkinter gui

[英]Make the tkinter gui update live

I'm making a Bitcoin price GUI.我正在制作比特币价格 GUI。 And I was wondering, is there any way to make the price update live?我想知道,有什么方法可以实时更新价格?

page = urllib.request.urlopen("https://www.coindesk.com/price/bitcoin").read()
html = BeautifulSoup(page, "html.parser")
btcClass = html.find(class_="price-large")
btcClass1 = str(f"{btcClass}$")
btcClass2 = btcClass1[54:63]
Label1 = tkinter.Label(text=f"BTC\n{btcClass2}", font=("Arial", 25)).pack()

def Update():
    #price update

Update()

You could use the .after() method.您可以使用.after()方法。

def Update(
     ... code that gets prices and updates screen)
     root.after(1000, Update)

this will update your screen every 1000ms (1 second)这将每 1000 毫秒(1 秒)更新一次屏幕



Update():
      html = BeautifulSoup(page, "html.parser")
      btcClass = html.find(class_="price-large")
      btcClass1 = str(f"{btcClass}$")
      btcClass2 = btcClass1[54:63]
      label.config(text=f"BTC\n{btcClass2}"
      root.after(1000, Update)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM