如何使用while循环tkinter无限更新标签 [英] How to Infinitely Update Labels with while loop tkinter

查看:156
本文介绍了如何使用while循环tkinter无限更新标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一些简单的代码. https://pastebin.com/97uuqKQD

Hi I have some simple code here. https://pastebin.com/97uuqKQD

我只想在按钮功能中添加这样的内容,因此,在根窗口打开时,日期时间和exrate会不断从xe网站中删除并显示.

I would simply like to add something like this, to the button function, so while the root window is open the datetime and exrates are constantly regotten from the xe website and displayed.

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)

一旦我输入了两个比率,然后它们分别显示在标签上,我希望程序不断从xe连续获取数据.

Once I input the two exrates and they then display on there respective labels I would like the program to continuously grab the data from xe over and over again.

就像这里的在IPython中运行的代码一样,

Like this code here which runs in IPython no problems,

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循环函数中,然后简单地调用该函数,但是没有任何帮助将不胜感激.?

I thought I would just be able to throw the entire results function into a while loop function then simply call that function but no luck any help would be appreciated.???

推荐答案

将其放在结果函数的末尾,而无需while循环:

Put this at the end of your results function without the while loop:

after_id = root.after(milliseconds,results) 

这样,它仅在您指定的时间之后继续运行.并且此代码将取消它.

This way it just keeps running itself after that time you specified. And this code will cancel it.

root.after_cancel(after_id)


在评论中回答您的其他问题:
要创建一个取消按钮,请确保after_id是全局的.同样,如果您指定的时间很短(非常快的召回时间),它可能会再次重新启动,然后才能取消.因此,为了安全起见,最好使全局布尔值并将.after放在if boolean == True中.您可以在每次按下取消"按钮时将该布尔值设置为False,或者在每次按下开始"按钮时将该布尔值设置为True,这是您可以执行的操作:


Answering your other question in the comments:
To make a cancel button make sure after_id is global. Also if the time you specify is very low (very fast recall) it might restart again before you can cancel it. So to be safe better make a global boolean and put .after in an if boolean==True. And you can set that boolean to False whenever you hit cancel button or to True whenever you hit the start button, here's how you can do it:

# 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

现在您可以将其放入取消按钮功能:

Now you can put this in your cancel button function:

def cancel():
    global restart
    global after_id
    root.after_cancel(after_id)
    restart = False

让我知道这是否可行(我自己没有测试过).

Let me know if this works (haven't tested it myself).

这篇关于如何使用while循环tkinter无限更新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆