Reputation: 263
I am testing the Python script for a simple timer but it's not showing anything in the Tkinter window.
import time
from tkinter import *
def clicked():
val = int(val_Box.get())
val1 = 0
global display_Time
while val1 < val:
display_Time.configure(text=str(val))
display_Time = Label(window,text="",fg="Blue")
display_Time.grid(column=10,row=10)
time.sleep(1)
val -= 1
display_Time.configure(text="Timer Finished")
window = Tk()
window.title("Simple Timer App")
window.geometry("300x200")
clock_Time = Label(window,text= "Enter Clock Time",fg="Green")
clock_Time.grid(column=0,row=0)
val_Box = Entry(window,width = 10,bg = "Yellow")
val_Box.grid(column=1,row=0)
display_Time = Label(window,text="" ,fg="Blue")
display_Time.grid(column=2,row=10)
start_Button = Button(window,text = "START",fg = "Blue",command = clicked)
start_Button.grid(column=0,row=10)
window.mainloop()
The output is not showing the sequential decrease of the entered value. I need some suggestions.
This is how the timer looks like.
Upvotes: 1
Views: 211
Reputation: 36239
You need to update
the window in order to make the changes effective. You can so by calling window.update()
. Also you overwrite the label that's acting as your timer display during the while loop. There is no reason to do so. So you can safely remove the corresponding two lines. Effectively the rewritten while
loop should look like:
while val1 < val:
display_Time.configure(text=str(val))
window.update()
time.sleep(1)
val -= 1
Also note that the clicked
function will block your GUI thread until the timer has finished. It's probably more appropriate to start a separate timer thread such as from threading import Timer
(check out this answer for how to repeatedly perform a task - such as updating the GUI - with it).
Upvotes: 1