Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何使用while循环tkinter无限更新标签_Python 3.x_Button_Tkinter_Label_Infinite Loop - Fatal编程技术网

Python 3.x 如何使用while循环tkinter无限更新标签

Python 3.x 如何使用while循环tkinter无限更新标签,python-3.x,button,tkinter,label,infinite-loop,Python 3.x,Button,Tkinter,Label,Infinite Loop,嗨,我这里有一些简单的代码 我只想在button函数中添加类似的内容,这样当根窗口打开时,datetime和exrates会不断地从xe网站重新登录并显示出来 amount = '1' def continuousUpdate(): while amount == '0': results() def results(): #Get xe data put to labels etc here btnConvert = tk.Button(root, tex

嗨,我这里有一些简单的代码

我只想在button函数中添加类似的内容,这样当根窗口打开时,datetime和exrates会不断地从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)
一旦我输入了两个exrate,然后它们显示在各自的标签上,我希望程序不断地从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
让我知道这是否有效(我自己没有测试过)