I have been teaching Python for several months and am starting to learn some GUIs.
I wrote this simple script based on the pack_remove example that I found in the book. My script just displays local and UTC time every second. Of course, the only difference is the hour, I would still like to redraw every second.
The script is running, but my RAM is constantly increasing with every impression. I start with 4 MB, then after 2 hours or so the script uses 25 MB. It does matter to me, but I was curious if there is a way to display new times every second, but reduce the memory usage of such a simple clock display.
Or am I using an inefficient technique to redisplay data in a GUI at high frequency?
Here is my code:
from tkinter import * import time class TimeDisplay(Frame): def __init__(self,msecs = 1000): Frame.__init__(self) self.msecs = msecs self.pack() utc_time = Label(self, text='') utc_time.pack() cst_time = Label(self, text='') cst_time.pack() self.utc_time = utc_time self.cst_time = cst_time self.repeater() def repeater(self): self.utc_time.pack_forget() self.cst_time.pack_forget() self.utc_time = Label(self, text= 'UTC: ' + time.strftime('%Y/%m/%d %H:%M:%S',time.gmtime())) self.utc_time.pack() self.utc_time.config(bg='navy',fg='white') self.cst_time = Label(self, text= 'CST: ' + time.strftime('%Y/%m/%d %H:%M:%S',time.localtime())) self.cst_time.pack() self.cst_time.config(bg='navy',fg='white') self.after(self.msecs, self.repeater) if __name__ == '__main__': TimeDisplay(msecs=1000).mainloop()
Thanks in advance
source share