在Python GUI更新时遇到一些麻烦

时间:2014-01-30 09:10:15

标签: python user-interface tkinter python-idle

import sys
import time
from tkinter import *
from tkinter import messagebox

Gui = Tk()
Gui.title("Exercise Timer")

def jesse(derp):
    x=derp
    test = 0
    while x>=0 and x<=derp:
        if test == 0:
            Gui.after(1000,makeLabel(x))
            x= x-1
            if x==0:
                test = 1
        if test == 1:
            if x == 0:
                Gui.after(1000,makeLabel("brk pls"))
                x=x+1
            else:    
                Gui.after(1000,makeLabel(x))
                x=x+1

def makeLabel(texts):
    Gui.update()
    newlab = Label(text=texts).pack()

def stop():
    Gui.destroy()

mbutton = Button(text="10 seconds",command = lambda: jesse(10)).pack()
mbutton = Button(text="20 seconds",command = lambda: jesse(20)).pack()
mbutton = Button(text="30 seconds",command = lambda: jesse(30)).pack()
mbutton = Button(text="quit",fg="red",command = lambda: stop()).pack()

Gui.mainloop()
sys.exit()

很抱歉,如果这是一个愚蠢的问题,我刚刚开始使用Python。基本上我的程序是从一个数字倒计时,然后计数每1秒显示下一个数字。这一切都很好,除了我去关闭它。它会关闭,但是jesse()函数会继续运行,并且在出现错误后会弹出一个窗口,其中只显示下一个数字,即使我已单击退出按钮或Windows x按钮。

1 个答案:

答案 0 :(得分:0)

import sys
import time
from Tkinter import *
import tkMessageBox

Gui = Tk()
Gui.title("Exercise Timer")

def jesse(derp):
    x=derp
    test = 0
    while x>=0 and x<=derp:
        if test == 0:
            Gui.after(1000,makeLabel(x))
            x= x-1
            if x==0:
                test = 1
        if test == 1:
            if x == 0:
                Gui.after(1000,makeLabel("brk pls"))
                x=x+1
            else:    
                Gui.after(1000,makeLabel(x))
                x=x+1

def makeLabel(texts):
    try:
        Gui.update()
        newlab = Label(text=texts).pack()
    except TclError:
        pass

def stop():
    Gui.destroy()

mbutton = Button(text="10 seconds",command = lambda: jesse(10)).pack()
mbutton = Button(text="20 seconds",command = lambda: jesse(20)).pack()
mbutton = Button(text="30 seconds",command = lambda: jesse(30)).pack()
mbutton = Button(text="quit",fg="red",command = lambda: stop()).pack()

Gui.mainloop()
sys.exit()

我添加了一个try和except块,以便它停止发出错误,我知道它不是一个非常整洁的修复,但我不清楚为什么你的代码甚至会出错。 我希望这有助于对不起,如果它没有

相关问题