嗨,我在這裏有一些簡單的代碼。 https://pastebin.com/97uuqKQD如何使用while循環tkinter無限更新標籤
我只是想添加類似這樣的按鈕功能,所以當根窗口打開時,日期時間和擴展時間會不斷從xe網站恢復並顯示出來。
amount = '1'
def continuousUpdate():
while amount == '0':
results()
def results():
#Get xe data put to labels etc here
btnConvert = tk.Button(root, text="Get Exchange Rates",command=continuousUpdate).place(x=5,y=102)
一旦我輸入兩個exrates,然後他們顯示那裏各自的標籤,我想程序不斷從xe中一遍又一遍地獲取數據。
像這裏這樣的代碼,在IPython中運行沒有問題,
import requests
from bs4 import BeautifulSoup
from datetime import datetime
amount = '1'
while amount != '0':
t = datetime.utcnow()
url1 = "http://www.xe.com/currencyconverter/convert/" + "?Amount=" + amount + "&From=" + cur1 + "&To=" + cur2
url2 = "http://www.xe.com/currencyconverter/convert/" + "?Amount=" + amount + "&From=" + cur2 + "&To=" + cur1
#url = "http://www.xe.com/currencycharts/" + "?from=" + cur1 + "&to=" + cur2
html_code1 = requests.get(url1).text
html_code2 = requests.get(url2).text
soup1 = BeautifulSoup(html_code1, 'html.parser')
soup2 = BeautifulSoup(html_code2, 'html.parser')
i = i + 1
rate1 = soup1.find('span', {'class', 'uccResultAmount'})
rate2 = soup2.find('span', {'class', 'uccResultAmount'})
print ('#',i, t,'\n', cur1,'-',cur2, rate1.contents[0], cur2,'-',cur1, rate2.contents[0], '\n')
我想我也只是能夠引發整個結果函數成while循環功能,然後簡單地調用該函數,但沒有運氣任何幫助將不勝感激。