Shade Reogen
Shade Reogen

Reputation: 146

Tkinter removing old Frame when updated

I tried this, and this method didn't help either.

I have a bit of code that shows current weather and temperature, and want it to obviously update. I manage to do the update, but it adds the updated frame underneath the old one. I can't seem to find a way to remove the old frame and replace it with the updated one.

Here's the extract of my code:

def refresh():
    Label(framewet, text=weatherstring + ",   " + temperature + "°",
          bg='black', fg='white', font=("avenir", 35)).pack()
    framewet.pack(side='right', fill='y')
    window.after(3000, refresh) # 3 secs just for debug
    # framewet.destroy() does not work, nor .pack_forget()

# window is the wholw window, may known as root.Tk()


if __name__ == "__main__":
    window.after(0, refresh)
    window.mainloop(0)

Thank you.

Upvotes: 0

Views: 282

Answers (1)

Mike - SMT
Mike - SMT

Reputation: 15226

Instead of destroying your frame you can simply update the label. This can be done with defining the label on init and then just updating it with config() on a loop.

Here is a simple counter with your code.

from tkinter import *


def refresh():
    global counter, label
    counter += 1
    label.config(text="Counter: {}".format(counter))
    window.after(1000, refresh)

if __name__ == "__main__":
    window = Tk()
    counter = 1
    framewet = Frame()
    framewet.pack(side='right', fill='y')
    label = Label(framewet, text="Counter: {}".format(counter), bg='black', fg='white', font=("avenir", 35))
    label.pack()
    refresh()
    window.mainloop()

Upvotes: 2

Related Questions