-1

I have seen similar answers even on stackoverflow but i cant solve my problem. This is my first program doing something specific. Its for Windows users only (windows 7 and higher). When you launch it in first Entry input minutes and press START button. This starts counting. Second Entry is for password. When typed 'dark' and STOP pressed it stops shutdown. And when im closing application with X button it gives me this:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1482, in __call__
    return self.func(*args)
  File "C:\MyWorld\newIdea.py", line 26, in startCount
    root.update()
  File "C:\Python34\lib\tkinter\__init__.py", line 965, in update
    self.tk.call('update')
_tkinter.TclError: can't invoke "update" command: application has been destroyed

Can you help me handle this exception? Or maybe edit my code to avoid this error?

import sys, time, os
from tkinter import *
import tkinter as tk

def change_1():
    B1['state'] = tk.DISABLED
    B2['state'] = tk.NORMAL   

def change_2():
    B1['state'] = tk.NORMAL
    B2['state'] = tk.DISABLED

def change_3():
    B1['state'] = tk.DISABLED
    B2['state'] = tk.DISABLED

def startCount():
    setTime = setMinutes.get() * 60
    strTime = str(setTime)
    timeSet = ("\""+"shutdown /s /f /t " +strTime+"\"")
    os.system(timeSet)
    change_1()
    for t in range(setTime, -1, -1):
        lcd = "{:02d}:{:02d}".format(*divmod(t, 60))
        timeString.set(lcd)
        root.update()
        time.sleep(1)
    return

def stopCount():
    passwd = "dark"
    passwdGet = getPass.get()
    if passwd != passwdGet:
        messagebox.showinfo('Wrong', 'Its not correct password')
        change_3()
    else:
        messagebox.showinfo('Good', 'Turn off canceled')
        os.system("shutdown /a")
        change_2()
    return

root = tk.Tk()
setMinutes = IntVar()
getPass = StringVar()
timeString = StringVar()
label_font = ('Verdana', 30)
root.geometry('260x150+200+200')
root.title('Timer v1.4')
root.resizable(0, 0)

L1 = Label(root, text='How much time You have?').grid(row=0, column=1)

L2 = Label(root, textvariable=timeString, font=label_font, bg='white', 
         fg='orange', relief='raised', bd=3)
L2.grid(row=1, columnspan=3, sticky='WE', padx=5, pady=5)

E1 = Entry(root, textvariable=setMinutes).grid(row=2, column=1, padx=5, pady=5)

B1 = Button(root, text='S T A R T', fg='green', bg='black', command=startCount)
B1.grid(row=2, rowspan=2, sticky='NS', column=0, padx=5, pady=5)

E2 = Entry(root, textvariable=getPass).grid(row=3, column=1, padx=5, pady=5)

B2 = Button(root, text='S T O P', fg='red', bg='black', command=stopCount,
            state=tk.DISABLED)
B2.grid(row=2, rowspan=2, sticky='NS', column=2, padx=5, pady=5)

root.mainloop()
guest013
  • 33
  • 1
  • 1
  • 11
  • There are better ways to do timers than with an infinite look and `sleep`. There are many examples on this site. Just search for `[tkinter] timer`. For example: http://stackoverflow.com/q/2400262/7432 – Bryan Oakley Nov 03 '16 at 23:46
  • You are right, and ive seen this example before but i dont know how write this code without class. OOP is important but every newbie self learner starts from functions – guest013 Nov 04 '16 at 04:45
  • Can somebody show me how do this right? Everything else in my program is ok, timer only needs to be corrected – guest013 Nov 04 '16 at 06:30
  • this is great. really great. somone new to python asks and some smart guy gives minus grade for that question and newguy is left without answer. just great. – guest013 Nov 04 '16 at 23:05
  • I'm sorry you feel that way, but I know how you feel. I know I certainly think "this is great. really great. Another person asking about a timer for the 100th time". The answers are all around you if you learn from them instead of just copying and pasting the code. http://stackoverflow.com/a/34029360/7432 is the third result when searching for "[tkinter] timer"; it doesn't use classes. – Bryan Oakley Nov 04 '16 at 23:50
  • The only thing i need is calm. Keep calm and keep coding ;) – guest013 Nov 05 '16 at 17:31

1 Answers1

0

Heres the answer:

def startCount():
    setTime = setMinutes.get() * 60
    strTime = str(setTime)
    timeSet = ("\""+"shutdown /s /f /t " +strTime+"\"")
    os.system(timeSet)
    change_1()
    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', 'Application terminated')
            return
        time.sleep(1)
    return

Now error does not show up. But new clean tk window appear :) Maybe next time i will on my own get right answer

guest013
  • 33
  • 1
  • 1
  • 11