We use some essential cookies to make our website work.

We use optional cookies, as detailed in our cookie policy, to remember your settings and understand how you use our website.

4 posts • Page 1 of 1
timkendal
Posts: 5
Joined: Thu Aug 12, 2021 7:25 pm

Trouble with root.mainloop and while True: in Tkinter app

Thu Jul 03, 2025 8:25 pm

I'm having trouble with my Python code, which uses Tkinter.
I have an LCD display attached to my Pi 400, & it works fine.
My app, which I've shortened in the Code below to iso;ate the problem, should
1. Display the Tkinter window, which includes a Quit button
2. Send data to the LCD (in the full version it's System Stats, but in thhe attached code it is just the time and a flashing asterisk

As I have not been able to get this working properly, displaying the data on the LCD and being able to quit using the button on the Tkinter window, I haave created 2 versions of the code, both attached, with notes at the end. Version 1 uses Root.mainloop(), and version 2 uses while True:,

I think the problem is my not understanding exactly how these 2 alternatives (root.mainloop and while True:) work. Maybe I need to structure things differrently (perhaps with threading, though I've never tried that I'm afraid)

Any help would be greatly appreciated.

Code: Select all

#. Version 1. root.mainloop()
import tkinter as tk
from datetime import datetime
import time
from rpi_lcd import LCD
lcd = LCD()
ct=1
 
root = tk.Tk()
root.geometry("500x200+250+30") # size then position
root.title("System Stats display on LCD")
 
tk.Label(root, text="System Stats now on display. \nClick Quit to go back to Choice Menu").grid(row=1,column = 0,sticky=tk.W, padx = 20, pady=40)
tk.Button(root, text='Quit', command=root.quit).grid(row=3, column=0,padx=250, pady=10)
 
 # while True:
now = datetime.now()
clockdis = " " +str(now.strftime("%H:%M"))
lcd.text(clockdis,1)
 
if ct==1:
 ast = " "
 lcd.text("Running" + ast ,2)
 ct=0
else:
 ast = " *"
 lcd.text("Running" + ast ,2)
 ct = 1 
 
time.sleep(0.3 )
root.mainloop()
''''
NOTES
Runs and Tk Window appears
Quit button works as it should. So the window is working as it should. 
BUT LCD Display updates only once, presumably as no looping?
My solution was to use 'while True:' to create a loop. See other code for result
'''

Code: Select all

# VERSION 2. while True:
import tkinter as tk
from datetime import datetime
import time
from rpi_lcd import LCD
lcd = LCD()
ct=1
 
root = tk.Tk()
root.geometry("500x200+250+30") # size then position
root.title("System Stats display on LCD")
 
tk.Label(root, text="System Stats now on display. \nClick Quit to go back to Choice Menu").grid(row=1,column = 0,sticky=tk.W, padx = 20, pady=40)
tk.Button(root, text='Quit', command=root.quit).grid(row=3, column=0,padx=250, pady=10)
 
while True:
 now = datetime.now()
 clockdis = " " +str(now.strftime("%H:%M"))
 lcd.text(clockdis,1)
 
 root.update() #without this the tkinter window does not appear. Not sure why!
 
 if ct==1:
 ast = " "
 lcd.text("Running" + ast ,2)
 ct=0
 else:
 ast = " *"
 lcd.text("Running" + ast ,2)
 ct = 1 
 
 time.sleep(0.3 )
 #root.mainloop()
'''
NOTES
Runs and Tk Window appears
Quit button does NOT work, though it sometimes changes colour on hover & depresses when clicked. 
I have to use the terminal window which also opens, and close that or Ctrl-C, to quit 
LCD Display updates as required, with flashing asterisk to show activity. So this part is doing what I want.
'''
Thanks in advance

Tim Kendal

thagrol
Posts: 14786
Joined: Fri Jan 13, 2012 4:41 pm

Re: Trouble with root.mainloop and while True: in Tkinter app

Thu Jul 03, 2025 8:50 pm

root.mainloop() never returns.

You call it once and it calls the functions you told it too when creating the widgets. If you don't call it, your widgets don't work.

If you want to do something in your code that is independent of tkinter events you need to put it in a separate thread.
Knowledge, skills, & experience have value. If you expect to profit from someone's you should expect to pay for them.

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

scotty101
Posts: 4585
Joined: Fri Jun 08, 2012 6:03 pm

Re: Trouble with root.mainloop and while True: in Tkinter app

Fri Jul 04, 2025 11:45 am

A separate thread isn't necessary and can be tricky to get working.

Presumably you want to update the LCD periodically, if that is the case, I'd recommend using tkinter's "after" method to schedule a function to run after a specified delay. At the end of the function, it then calls to schedule itself to run again after another delay.

For many tkinter applications this does the job perfectly and separate threads aren't needed (and are frankly a PITA).
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

timkendal
Posts: 5
Joined: Thu Aug 12, 2021 7:25 pm

Re: Trouble with root.mainloop and while True: in Tkinter app

Fri Jul 04, 2025 5:06 pm

Thanks for the replies - much appreciated!

As mentioned in the OP I have not used threads before, though I thought it might be on way forward. I've had a first try, but without success so far. I'll try again

As to the 'after' solution, I'll give this a try as well, though I'm not sure it will work as the LCD display includes the time and I need it to be updated often. But a very short 'after' delay might work - I'll see what I can do!

4 posts • Page 1 of 1

Return to "Troubleshooting"

AltStyle によって変換されたページ (->オリジナル) /