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

查看:31
本文介绍了如何使用 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()

<小时>

顺便说一句:你可以使用 StringVar 作为 sundar nataraj Сундар 建议

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

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