0

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.

figbeam
  • 7,001
  • 2
  • 12
  • 18
Shade Reogen
  • 136
  • 2
  • 13
  • 1
    Why are you destroying the frame instead of simply updating the label? – Mike - SMT Dec 17 '18 at 16:51
  • There is a common theme across your questions. You do not provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). Some of your code is unknown so it becomes difficult to troubleshoot when the obvious is not fixing the problem here. – Mike - SMT Dec 17 '18 at 17:25
  • @Mike-SMT I can't upload the whole code, it's huge and shared between multiple file.. I thought the error-giving bit would suffice. I'm sorry for the inconvenience. I'll try to fix my problem and provide full functional code the next time. Thank you – Shade Reogen Dec 17 '18 at 17:32
  • I am not saying to upload your whole code. But you need to build a MCVE so we have something testable. Your post are not testable as key information is missing. You need to read the link provided so you can understand what is required for a post here. You also did not provide an error. You need to also include your traceback error. – Mike - SMT Dec 17 '18 at 17:33

1 Answers1

2

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()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79