-1
即使在stackoverflow上我看到了類似的答案,但我無法解決我的問題。這是我第一個做特定事情的程序。它僅適用於Windows用戶(Windows 7和更高版本)。當您在第一個輸入輸入分鐘中啓動它時,按下START按鈕。這開始計數。第二項是密碼。當鍵入「黑暗」並按下STOP時,停止關機。當與X鍵IM收盤應用它給了我這樣的:tkinter中的計時器 - 給出錯誤說「無法調用」更新「命令:應用程序已被破壞」
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
你能幫助我處理這個異常?或者可能編輯我的代碼以避免此錯誤?
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()
有更好的方法來做定時器比無限的外觀和'睡眠'。這個網站上有很多例子。只要搜索'[tkinter]計時器'。例如:http://stackoverflow.com/q/2400262/7432 –
你是對的,我以前見過這個例子,但我不知道如何寫這段代碼沒有類。面向對象是重要的,但每個新手自學習從函數開始 – guest013
有人可以告訴我這是怎麼回事?我的程序中的其他一切都可以,定時器只需要更正 – guest013