如何使用Tkinter创建自动更新的GUI? [英] How do I create an automatically updating GUI using Tkinter?

查看:195
本文介绍了如何使用Tkinter创建自动更新的GUI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from Tkinter import *
import time
#Tkinter stuff

class App(object):
    def __init__(self):
        self.root = Tk()

        self.labeltitle = Label(root, text="",  fg="black", font="Helvetica 40 underline bold")
        self.labeltitle.pack()

        self.labelstep = Label(root, text="",  fg="black", font="Helvetica 30 bold")
        self.labelstep.pack()

        self.labeldesc = Label(root, text="",  fg="black", font="Helvetica 30 bold")
        self.labeldesc.pack()

        self.labeltime = Label(root, text="",  fg="black", font="Helvetica 70")
        self.labeltime.pack()

        self.labelweight = Label(root, text="",  fg="black", font="Helvetica 25")
        self.labelweight.pack()

        self.labelspeed = Label(root, text="",  fg="black", font="Helvetica 20")
        self.labelspeed.pack()

        self.labeltemp = Label(root, text="", fg="black", font="Helvetica 20")
        self.labeltemp.pack()

        self.button = Button(root, text='Close recipe', width=25, command=root.destroy)
        self.button.pack()

    def Update(self, label, change):
        label.config(text=str(change))

def main():
    app = App()
    app.mainloop()

if __name__ == "__main__":
    main()

我正在尝试创建一个配方显示在Tkinter GUI的屏幕上显示步骤,说明,重量和其他变量。

I'm trying to create a recipe display which will show the step, instructions, weight and other variables on a screen in a Tkinter GUI.

但是,我不知道如何在每次新步骤中更新GUI以进行更改配方,因为必须根据用户输入(从服务器获取)动态更新内容。如何根据步骤的更改实现GUI其他元素的更新?

However, I do not know how to update the GUI to change with each new step of the recipe, as the content has to be dynamically updated based on user input (taken from a server). How can I achieve updating of the GUI's other elements based on the change in steps?

推荐答案

您可以使用 after()在(例如)1000毫秒(1秒)后运行功能以执行某项操作并更新标签上的文本。此功能可以在1000毫秒后再次运行。

You can use after() to run function after (for example) 1000 miliseconds (1 second) to do something and update text on labels. This function can run itself after 1000 miliseconds again (and again).

这是当前时间的示例

from Tkinter import *
import datetime

root = Tk()

lab = Label(root)
lab.pack()

def clock():
    time = datetime.datetime.now().strftime("Time: %H:%M:%S")
    lab.config(text=time)
    #lab['text'] = time
    root.after(1000, clock) # run itself again after 1000 ms

# run first time
clock()

root.mainloop()






BTW :您可以将 StringVar 用作 sundar natarajСундар建议

这篇关于如何使用Tkinter创建自动更新的GUI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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