python-使用tkinter和x27之间的延迟;s列表框打印
我只想在GUI中包含的列表框上的每次打印之间做一个时间延迟。 到目前为止,我还没有取得结果。我试图使用time.sleep()。。。和之后的方法 这是我的密码:python-使用tkinter和x27之间的延迟;s列表框打印,python,user-interface,tkinter,Python,User Interface,Tkinter,我只想在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=En
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时,应该始终使用而不是睡眠。如果您处于睡眠状态,GUI将停止更新,因此您将无法刷新显示,并且任何东西都无法正常工作 要在代码中获得所需的结果,您有几个选项。其中之一是在之后使用
调用插入列表框的函数,并为每次迭代向其传递一个新参数
首先,必须修改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
,则还需要使函数返回
,否则它将永远运行;) 您应该查看线程
模块。