你如何在 Tkinter 中刷新窗口 [英] How do you refresh a window in Tkinter

查看:269
本文介绍了你如何在 Tkinter 中刷新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我用一些填充整个窗口的文本创建了 Tkinter 窗口,现在想用新文本替换窗口,有没有办法刷新窗口?

If I created Tkinter window with some text that filled the whole window and now wanted to replace the window with a new text, is there a way to refresh the window?

例如:

    a= 100
    win= Tk() 
    win.geometry("500x300")
    while a > 0:
       if a%2 == 0:
           lbl = Label (win, bg = "purple")
           lbl.pack()
       else:
           lbl = Label (win, bg = "blue")
           lbl.pack()
       a= x-1

此代码的问题在于 Tkinter 窗口不会刷新,只会提供最终结果,而不是显示窗口更改颜色.谢谢您的帮助!

The problem with this code is that the Tkinter window does not refresh and just provides the end result instead of showing the windows changing colors. Thanks for the help!

推荐答案

那不是改变 UI 状态的方法,因为即使你刷新窗口它也会很快你不会注意到,而是改变状态,等待一段时间并再次更改状态,例如在这里,我展示了如何为颜色设置动画

That is not the way to change UI states, because even if you refreshed the window it would be so quick you won't notice, instead change the state, wait some time and change the state again e.g. here I show how to animate color

from Tkinter import *

index = 0
def changeColor():
    global index
    if index%2==0:
        label.configure(bg = "purple")
    else:
        label.configure(bg = "blue")
    index+=1
    label.after(1000, changeColor)

root = Tk()
mainContainer = Frame(root)
label = Label(mainContainer, text="")
label.configure(text="msg will change every sec")
label.pack(side=LEFT, ipadx=5, ipady=5)
mainContainer.pack()
label.after(1000, changeColor)
root.title("Timed event")
root.mainloop()

这篇关于你如何在 Tkinter 中刷新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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