如何使用 while 循环 tkinter 无限更新标签
How to Infinitely Update Labels with while loop tkinter
嗨,我这里有一些简单的代码。 https://pastebin.com/97uuqKQD
我只是想在按钮功能中添加这样的东西,所以当根 window 打开时,日期时间和汇率不断从 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 循环函数中,然后简单地调用该函数,但运气不好,我们将不胜感激。???
将其放在没有 while 循环的结果函数的末尾:
after_id = root.after(milliseconds,results)
这样它只会在您指定的时间后保持 运行 本身。而这段代码将取消它。
root.after_cancel(after_id)
在评论中回答你的其他问题:
要制作取消按钮,请确保 after_id 是全局的。此外,如果您指定的时间非常短(召回速度非常快),它可能会在您取消之前再次重新启动。所以为了安全起见,最好创建一个全局布尔值并将 .after 放在 if boolean==True 中。您可以在点击取消按钮时将该布尔值设置为 False,或者在您点击开始按钮时将该布尔值设置为 True,方法如下:
# button_call default will be True so if you click on the button
# this will be True (no need to pass var through). You can use this to set
# your restart boolean to True
def func(button_call=True):
global restart
global after_id
if button_call:
restart = True
if restart:
after_id = root.after(ms,lambda: func(button_call=False) )
# This way you know that func was called by after and not the button
现在您可以将其放入取消按钮功能中:
def cancel():
global restart
global after_id
root.after_cancel(after_id)
restart = False
让我知道这是否有效(我自己还没有测试过)。
嗨,我这里有一些简单的代码。 https://pastebin.com/97uuqKQD
我只是想在按钮功能中添加这样的东西,所以当根 window 打开时,日期时间和汇率不断从 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 循环函数中,然后简单地调用该函数,但运气不好,我们将不胜感激。???
将其放在没有 while 循环的结果函数的末尾:
after_id = root.after(milliseconds,results)
这样它只会在您指定的时间后保持 运行 本身。而这段代码将取消它。
root.after_cancel(after_id)
在评论中回答你的其他问题:
要制作取消按钮,请确保 after_id 是全局的。此外,如果您指定的时间非常短(召回速度非常快),它可能会在您取消之前再次重新启动。所以为了安全起见,最好创建一个全局布尔值并将 .after 放在 if boolean==True 中。您可以在点击取消按钮时将该布尔值设置为 False,或者在您点击开始按钮时将该布尔值设置为 True,方法如下:
# button_call default will be True so if you click on the button
# this will be True (no need to pass var through). You can use this to set
# your restart boolean to True
def func(button_call=True):
global restart
global after_id
if button_call:
restart = True
if restart:
after_id = root.after(ms,lambda: func(button_call=False) )
# This way you know that func was called by after and not the button
现在您可以将其放入取消按钮功能中:
def cancel():
global restart
global after_id
root.after_cancel(after_id)
restart = False
让我知道这是否有效(我自己还没有测试过)。