'Tkinter: Main window is not loading
I'm currently learning to use Tkinter library in python, so to do that, I decided to do a few projects using it, right now I'm trying to make a digital clock using "Tkinter and time" libraries.
So far I'm aware of a bug in my code, and I'm looking for a solution to this bug.
Bug: I use three for loops inside each other to count the seconds/minutes/hours and change them
accordingly (every one second the seconds variable (which I named secs) get added a 1 to it) etc...
So I think that the problem is that it's taking too long for the loops to finish and for the program to reach the root.mainloop() line of code.
Code:
import tkinter
import time
# Setting the main setting in tkinter #
root = tkinter.Tk()
# Sizes of the window #
root.geometry("600x400")
root.resizable(False, False)
# Changing the title and the icon of the root #
root.title("Digital Clock")
root.iconbitmap("Digital clock icon.ico")
# Drawing the digital clock on the screen #
secs = 0
mins = 0
hrs = 0
label = tkinter.Label(root, text=f"0{hrs}:0{mins}:0{secs}", font=("helvetica", 38))
label.pack(ipadx=10, ipady=150)
for h in range(1, 24):
for m in range(1, 60):
for s in range(1, 60):
hrs = h
mins = m
secs = s
# Organize the string in the right format #
if secs < 10 and mins < 10 and hrs < 10:
label.config(text=f"0{hrs}:0{mins}:0{secs}")
elif secs < 10 <= hrs and mins < 10:
label.config(text=f"{hrs}:0{mins}:0{secs}")
elif secs < 10 <= mins and hrs >= 10:
label.config(text=f"{hrs}:{mins}:0{secs}")
elif secs >= 10 and mins >= 10 and hrs >= 10:
label.config(text=f"{hrs}:{mins}:{secs}")
time.sleep(1)
# The main loop #
root.mainloop()
Solution 1:[1]
It looks like your loop is running infinitely that is why tkinter can't reach to root.mainloop() which is after the loop
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vivek Singh |
