【问题标题】:Having some trouble with Python GUI updatePython GUI 更新有一些问题
【发布时间】:2014-01-30 09:10:14
【问题描述】:
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 按钮。

【问题讨论】:

    标签: python user-interface tkinter python-idle


    【解决方案1】:

    "

    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 块,以便它停止给出错误,我知道它不是一个非常整洁的修复,但我不清楚为什么你的代码甚至会给出错误。 如果没有,我希望这会有所帮助,抱歉

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-13
      • 1970-01-01
      • 1970-01-01
      • 2013-04-23
      • 2011-03-26
      • 1970-01-01
      • 2011-12-16
      相关资源
      最近更新 更多