【问题标题】:Timer in tkinter - gives Error saying “can't invoke ”update“ command: application has been destroyed”tkinter 中的计时器-给出错误说“无法调用”更新“命令:应用程序已被破坏”
【发布时间】:2016-11-02 16:59:49
【问题描述】:

即使在 stackoverflow 上我也看到过类似的答案,但我无法解决我的问题。这是我第一个做特定事情的程序。它仅适用于 Windows 用户(Windows 7 及更高版本)。当您在第一个条目输入分钟时启动它并按开始按钮。这开始计数。第二项是密码。当输入“dark”并按下 STOP 时,它会停止关机。当我用 X 按钮关闭应用程序时,它给了我这个:

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

【问题讨论】:

  • 有比无限外观和sleep 更好的计时器方法。这个网站上有很多例子。只需搜索[tkinter] timer。例如:stackoverflow.com/q/2400262/7432
  • 你是对的,我以前看过这个例子,但我不知道如何在没有类的情况下编写这段代码。 OOP 很重要,但每个新手自学都是从函数开始
  • 有人能告诉我怎么做吗?我程序中的其他一切都可以,只需要更正计时器
  • 这很棒。非常好。 python 新手问,一些聪明人给这个问题打了负分,而 newguy 没有答案。太好了。
  • 很抱歉您有这种感觉,但我知道您的感受。我知道我当然认为“这很棒。真的很棒。另一个人第 100 次询问计时器”。如果您向他们学习而不是仅仅复制和粘贴代码,那么答案就在您身边。 stackoverflow.com/a/34029360/7432 是搜索“[tkinter] timer”时的第三个结果;它不使用类。

标签: python


【解决方案1】:

答案如下:

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

现在错误没有出现。但是出现了新的干净的 tk 窗口:) 也许下次我会自己得到正确的答案

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 2022-08-03
    • 2013-10-23
    • 1970-01-01
    • 2011-09-02
    • 2014-05-12
    相关资源
    最近更新 更多