Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.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/3/templates/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中的嵌套函数_Python - Fatal编程技术网

需要python中的嵌套函数

需要python中的嵌套函数,python,Python,我熟悉c/c++,对python还不熟悉,我想知道python中的嵌套函数需要什么。有谁能给出一个我们使用嵌套函数的实例吗。提前感谢。您的问题太广泛,因此可能会被关闭,但有两个示例是和。当您想使用帮助函数(并且不想提供参数),尤其是线程时: import Tkinter as tk import thread def main(): root = tk.Tk() progress_var = tk.StringVar(root) progress_var.set('')

我熟悉c/c++,对python还不熟悉,我想知道python中的嵌套函数需要什么。有谁能给出一个我们使用嵌套函数的实例吗。提前感谢。

您的问题太广泛,因此可能会被关闭,但有两个示例是和。

当您想使用帮助函数(并且不想提供参数),尤其是线程时:

import Tkinter as tk
import thread

def main():
    root = tk.Tk()
    progress_var = tk.StringVar(root)
    progress_var.set('')
    progress = tk.Label(root, textvariable = progress_var)
    progress.pack()
    def thread_helper(): # this can access the local variables in main()
        total = 10000000
        for i in xrange(1, total + 1):
             progress_var.set('Progress: {}%'.format(100 * float(i)/total))
    thread.start_new_thread(thread_helper, ())
    root.mainloop()
这实际上是在窗口中显示
for
循环的进度。在
root.mainloop()
之后,将不会运行任何进程,因此我需要使用一个线程来实际启动
for
循环,并更新进度显示

线程需要调用一个函数,只定义一个helper函数(嵌套且只使用一次)要比创建一个新函数并传递各种参数容易得多