繁体   English   中英

python-使用tkinter的列表框打印之间的延迟

[英]python- using delay between tkinter's listbox prints

我要做的就是在GUI上包含的列表框中的每次打印之间进行时间延迟。 到目前为止,我还没有得到结果。 我试图使用time.sleep()...和之后的方法。

这是我的代码:

from Tkinter import *

def func() :
    i = int (e.get())
    for x in range (0,i):
        listbox.insert (END, i)
        i-=1

master = Tk()
master.title("hi") 

e=Entry (master )
e.pack()

listbox = Listbox(master)
listbox.pack()

b = Button(master, text="get", width=10, command=func)
b.pack()

mainloop()

使用GUI时,应始终使用after而不是sleep。 如果您睡着了,GUI将停止更新,因此您将不会得到刷新的显示,并且任何操作都无法实现。

为了在代码中获得理想的结果,您有几种选择。 其中之一将使用after来调用插入列表框的函数,并为每次迭代将新的参数传递给该函数。

首先,您必须修改Button命令,以使用lambda表达式将初始参数传递给函数:

b = Button(master, text="get", width=10, command=lambda: func(int(e.get())))

接下来,像这样构造函数:

def func(arg):
    listbox.insert(END, arg)      #insert the arg
    arg -= 1                      #decrement arg
    master.after(1000, func, arg) #call function again after x ms, with modified arg

注意:如果arg小于0 ,则还需要使函数return ,否则它将永远运行;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM