Python Forum
[Tkinter] Change Label Every 5 Seconds
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Change Label Every 5 Seconds
#1
I have written a loop that changes a Label. However, I don't know how to wait until the changed label is displayed to start my time delay. If I change the Label then immediately time.sleep(5), the label never gets changed. If I remove the sleep and go though the loop once, the label does get changed but obviously only 1 time. How can I accomplish this? TIA.
Reply
#2
See [Tkinter] How to deal with code that blocks the mainloop, freezing the gui
Reply
#3
Thanks for that link. I now see what to do but not how to do it. I am struggling trying to figure out what to use for 'after'. The window in question is saved as self.root. If I understand my code should simply be:
print('starting scan')
sensors=self.xml.getSensors()
scan=True
while (scan):
     junk,sensID=self.menu.getNextMenu()
     sens=functions.findID(sensors,sensID)
     print(sensID,sens.temp)
     self.updateLines(sensID,'  *tmp: '+ str(sens.temp))
     self.root.after(0)
     time.sleep(5)
Unfortunately that doesn't work so it is not as simple has I had hoped. The 'updateLines' method is:
def updateLines(self,first,second):
        self.drawLine1(first)
        self.drawLine2(second)

def drawLine1(self,str):
        self.line1=CL.CustomFont_Label(self.root,text=self.pad(str),font_path=se
lf.fontPath,size=36).grid(row=0,column=0,columnspan=5)

def drawLine2(self,str):
        self.line2=CL.CustomFont_Label(self.root,text=self.pad(str),font_path=se
lf.fontPath,size=36).grid(row=1,column=0,columnspan=5)
CustomFont_Label is from this recipe.
Reply
#4
It will be something more like this
    def scanning(self):
        junk,sensID=self.menu.getNextMenu()
        sens=functions.findID(self.sensors,sensID)
        print(sensID,sens.temp)
        self.updateLines(sensID,f'  *tmp: {sens.temp}')
        if self.scan:
            self.root.after(5000, self.scanning)
after uses a separate thread to call the passed in callable once the duration of milliseconds is up, the GUI will continue to function as normal.

Example
import tkinter as tk
import datetime


class MainFrame(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.label = tk.Label(self)
        self.label.pack()
        self.pack()
        self.every5sec()

    def every5sec(self):
        self.label['text'] = datetime.datetime.now()
        self.after(5000, self.every5sec)


if __name__ == '__main__':
    app = tk.Tk()
    main_frame = MainFrame()
    app.mainloop()
Reply
#5
Thanks. I think I see said the blind man as he picked up a hammer and saw.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [WxPython] [SOLVED] How to change button label? Winfried 3 2,020 May-31-2022, 06:37 PM
Last Post: Winfried
  [Tkinter] Remove label after x seconds snakes 3 4,214 Jun-11-2021, 09:20 PM
Last Post: snakes
  [Tkinter] Python 3 change label text gw1500se 6 4,608 May-08-2020, 05:47 PM
Last Post: deanhystad
  [Tkinter] Change label for multiple frames Dandy_Don 3 2,928 Apr-30-2020, 02:22 PM
Last Post: Dandy_Don
  [PyQt] Python PyQt5 - Change label text dynamically based on user Input ppel123 1 13,668 Mar-20-2020, 07:21 AM
Last Post: deanhystad
  [Tkinter] So what do I need to do change the color this label? jpezz 10 8,667 Apr-03-2019, 09:49 PM
Last Post: jpezz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020