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?

For Example:

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
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!

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?

For Example:

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
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!

Do you mean like this?

from Tkinter import *

def change(a=0):
    print a, ## debug
    lbl.config(bg = "blue" if a & 1 else "purple")
    lbl.after(400,change, a ^ 1 )

if __name__ == '__main__':
    win= Tk() 
    win.geometry("500x300")
    lbl = Label (win)
    lbl.pack(expand=YES, fill=BOTH)
    change()
    
    win.mainloop()

Edited 6 Years Ago by pyTony: n/a

This article has been dead for over six months. Start a new discussion instead.