Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Not proceeding with code after while loop

I’m trying to make a simple digital clock using Tkinter. However, after I use "while True" to update the variable and the label, it doesn’t create a window, even though that part is not indented. Here’s my code:

from datetime import datetime
from tkinter import *

root = Tk()
root.geometry('400x200')

while True
    now = datetime.now()

    current_time = now.strftime("%H:%M:%S")

    clock = Label(root, text = current_time)
    clock.pack()


root.update()
``

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

while True:
...

Program is stucked in this part it will keep on executing it.

In order to solve the issue move while True logic inside a thread.
Here’s another simple approach we are using clock.after(400, update) which will call after 400 milliseconds and update the label we are using mainloop to ensure that main will not exit till our window is not closed.

from datetime import datetime
from tkinter import *

root = Tk()
root.geometry('400x200')

clock = Label(root)
clock.pack()

def update():
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    print(current_time)
    clock.config(text=current_time)
    clock.after(400, update)



root.update()

update()

mainloop()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading