Hi, im writing app in Python 3.4 using tkinter. Its timer for windows 7 and higher. Input some minutes and press start button. When time reaches zero it will shutdown computer. Ive used update method with time.sleep - look at my code. But i need to make this timer with after method and i dont know how. root.update and time.sleep gives me TclError when X button is pressed (root.destroy). Ive tried many times without correct answer. Can You help me with my problem?
import time, os
import tkinter as tk
from tkinter import *
def startCount():
setTime = setMinutes.get() * 60
strTime = str(setTime)
timeSet = ("\""+"shutdown /s /f /t " +strTime+"\"")
os.system(timeSet)
for t in range(setTime, -1, -1):
lcd = "{:02d}:{:02d}".format(*divmod(t, 60))
timeString.set(lcd)
try:
root.update()
except TclError:
messagebox.showinfo('Info', 'Time is unstoppable.')
return
time.sleep(1)
return
root = tk.Tk()
setMinutes = IntVar()
timeString = StringVar()
label_font = ('Verdana', 30)
L1 = tk.Label(root, text='How much time you have?', anchor='center')
L1.grid(row=0, columnspan=4, sticky='WE')
L2 = Label(root, textvariable=timeString, font=label_font, bg='white',
fg='orange', relief='raised', bd=3)
L2.grid(row=1, columnspan=4, sticky='WE', padx=5, pady=5)
E1 = Entry(root, textvariable=setMinutes)
E1.grid(row=2, column=0, columnspan=2, sticky='WE', padx=5, pady=5)
B1 = Button(root, text='S T A R T', fg='green', bg='black', command=startCount)
B1.grid(row=2, column=3, padx=5, pady=5)
root.mainloop()
I tried to add this code with
setTime = setMinutes.get() * 60
as global and I put root.after call one code line before root.mainloop
def displayCountdown():
global setTime
for t in range (setTime, -1, -1):
lcd = "{:02d}:{:02d}".format(*divmod(t, 60))
timeString.set(lcd)
setTime -= 1
if setTime > 0:
root.after(1000,displayCountdown)
For this moment i do not know how do it correctly, so once again - Please help me