Python GUI upd有问题吗

2024-04-19 15:37:28 发布

您现在位置:Python中文网/ 问答频道 /正文

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()会继续运行,在出现错误后,会弹出一个窗口,其中只包含下一个数字,即使我单击了quit按钮或windowsx按钮。你知道吗


Tags: lambdatexttestimportifdefguibutton
1条回答
网友
1楼 · 发布于 2024-04-19 15:37:28

““

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-and-except块,这样它就不会出现错误了,我知道这不是一个很好的修复,但是我不清楚为什么你的代码会出现错误。 我希望这能帮上忙,如果不行的话

相关问题 更多 >