Trouble with root.mainloop and while True: in Tkinter app
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.
Thanks in advance
Tim Kendal
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.
'''
Tim Kendal
Re: Trouble with root.mainloop and while True: in Tkinter app
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.
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
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
Re: Trouble with root.mainloop and while True: in Tkinter app
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).
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
Pi Interests: Home Automation, IOT, Python and Tkinter
Re: Trouble with root.mainloop and while True: in Tkinter app
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!
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!
Jump to
- Community
- General discussion
- Announcements
- Other languages
- Deutsch
- Español
- Français
- Italiano
- Nederlands
- 日本語
- Polski
- Português
- Русский
- Türkçe
- User groups and events
- Raspberry Pi Official Magazine
- Using the Raspberry Pi
- Beginners
- Troubleshooting
- Advanced users
- Assistive technology and accessibility
- Education
- Picademy
- Teaching and learning resources
- Staffroom, classroom and projects
- Astro Pi
- Mathematica
- High Altitude Balloon
- Weather station
- Programming
- C/C++
- Java
- Python
- Scratch
- Other programming languages
- Windows 10 for IoT
- Wolfram Language
- Bare metal, Assembly language
- Graphics programming
- OpenGLES
- OpenVG
- OpenMAX
- General programming discussion
- Projects
- Networking and servers
- Automation, sensing and robotics
- Graphics, sound and multimedia
- Other projects
- Media centres
- Gaming
- AIY Projects
- Hardware and peripherals
- Camera board
- Compute Module
- Official Display
- HATs and other add-ons
- Device Tree
- Interfacing (DSI, CSI, I2C, etc.)
- Keyboard computers (400, 500, 500+)
- Raspberry Pi Pico
- General
- SDK
- MicroPython
- Other RP2040 boards
- Zephyr
- Rust
- AI Accelerator
- AI Camera - IMX500
- Hailo
- Software
- Raspberry Pi OS
- Raspberry Pi Connect
- Raspberry Pi Desktop for PC and Mac
- Beta testing
- Other
- Android
- Debian
- FreeBSD
- Gentoo
- Linux Kernel
- NetBSD
- openSUSE
- Plan 9
- Puppy
- Arch
- Pidora / Fedora
- RISCOS
- Ubuntu
- Ye Olde Pi Shoppe
- For sale
- Wanted
- Off topic
- Off topic discussion