Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python-使用tkinter和x27之间的延迟;s列表框打印_Python_User Interface_Tkinter - Fatal编程技术网

python-使用tkinter和x27之间的延迟;s列表框打印

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

我只想在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时,应该始终使用而不是睡眠。如果您处于睡眠状态,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
,则还需要使函数
返回
,否则它将永远运行;)

您应该查看
线程
模块。