Jack Smith
Jack Smith

Reputation: 17

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()

Upvotes: 0

Views: 170

Answers (2)

Jack Smith
Jack Smith

Reputation: 17

So I worked on this code today, and I found a solution, actually the code now works! For those who want to see the final code, here you go:

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"{hrs}:{mins}:{secs}", font=("helvetica", 38))
label.pack(ipadx=10, ipady=150)


def changeTime(hrs, mins, secs):

    if secs >= 60:
        mins += 1
        secs = 0
    if mins >= 60:
        hrs += 1
        mins = 0

    # The right format #
    if hrs >= 10:
        hrs = str(hrs)
    else:
        hrs = "0" + str(hrs)

    if mins >= 10:
        pass
    else:
        mins = "0" + str(mins)

    if secs >= 10:
        secs = str(secs)
    else:
        secs = "0" + str(secs)

    # Organize the string in the right format #
    label['text'] = f"{hrs}:{mins}:{secs}"

    hrs = int(hrs)
    mins = int(mins)
    secs = int(secs)
    secs += 1

    if hrs < 24:
        root.after(1000, changeTime, hrs, mins, secs)


root.after(0, changeTime, hrs, mins, secs)


# The main loop #
root.mainloop()

Explanation

  • So the difference between this code and the previous one is that in this code I'm using the '.after()', so what I'm doing is that after the program finishes loading, I send it to a function which is called "changeTime()" and in this function what I'm doing is updating the label that shows the time every one second (evey one second is done using the '.after()' method), and I check if the hours variable (which is called 'hrs') is smaller than 24, since I want this program to reach 24 hours at max, if that is true wait one second and then access the changeTime function again, and that will continue like a loop, until the hrs variable reaches 24.
  • Feel free to use this code for your projects, or to look for something that you may need for your code.

  • For those who want to use this code, I changed the icon of the root window to an image that I have in my computer, so you may want to delete that line of code or change the icon.

  • Also thanks to every one who helped me finish this project :)

Upvotes: 1

Vivek K. Singh
Vivek K. Singh

Reputation: 170

It looks like your loop is running infinitely that is why tkinter can't reach to root.mainloop() which is after the loop

Upvotes: 1

Related Questions