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.
[Tkinter] Change Label Every 5 Seconds
[Tkinter] Change Label Every 5 Seconds
|
May-26-2020, 04:11 PM
May-26-2020, 04:59 PM
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.
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()
May-26-2020, 05:32 PM
Thanks. I think I see said the blind man as he picked up a hammer and saw.
|
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
[WxPython] [SOLVED] How to change button label? | Winfried | 3 | 3,377 |
May-31-2022, 06:37 PM Last Post: Winfried |
|
[Tkinter] Remove label after x seconds | snakes | 3 | 6,102 |
Jun-11-2021, 09:20 PM Last Post: snakes |
|
[Tkinter] Python 3 change label text | gw1500se | 6 | 6,414 |
May-08-2020, 05:47 PM Last Post: deanhystad |
|
[Tkinter] Change label for multiple frames | Dandy_Don | 3 | 4,067 |
Apr-30-2020, 02:22 PM Last Post: Dandy_Don |
|
[PyQt] Python PyQt5 - Change label text dynamically based on user Input | ppel123 | 1 | 15,837 |
Mar-20-2020, 07:21 AM Last Post: deanhystad |
|
[Tkinter] So what do I need to do change the color this label? | jpezz | 10 | 12,088 |
Apr-03-2019, 09:49 PM Last Post: jpezz |
Users browsing this thread: 1 Guest(s)