[英]Constant Tkinter window update
作為第一個項目,我決定構建一個應用程序,該應用程序可以向我顯示當前的油價,因此我不必一直查看外匯圖表。
該應用程序的問題在於,“更新”循環僅每3秒打印一次油價,因此我知道此循環會不斷執行,但它不僅不會更新窗口中的文本,還會在外殼程序崩潰時將其崩潰打印出油價。
我嘗試使用多處理模塊,但沒有區別。
def window():
root = Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
mylabel = Label( root, text = "" )
mylabel.pack()
def update():
while True:
global string_price
request = requests.get( "http://www.biznesradar.pl/notowania/BRENT-OIL-ROPA-BRENT#1d_lin_lin" )
content = request.content
soup = BeautifulSoup( content, "html.parser" )
element = soup.find( "span", { "class": "q_ch_act" } )
string_price = ( element.text.strip() )
print( string_price )
mylabel.configure( text = str( string_price ) )
time.sleep( 3 )
root.after( 400, update )
mainloop()
.after
方法已經while True
和sleep
while True
完成了您想要的工作。 消除sleep
和sleep
while
並after
內部添加另一個以連續通話。
def custom_update():
global string_price
request = requests.get("http://www.biznesradar.pl/notowania/BRENT-OIL-ROPA-BRENT#1d_lin_lin")
content = request.content
soup = BeautifulSoup(content, "html.parser")
element = soup.find("span", {"class": "q_ch_act"})
string_price = (element.text.strip())
print(string_price)
mylabel.configure(text=str(string_price))
root.after(3000, custom_update) #notice it calls itself after every 3 seconds
custom_update() #since you want to check right after opening no need to call after here as Bryan commented
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.