Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.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 使用MapLambda销毁小部件tkinter_Python_Tkinter_Lambda - Fatal编程技术网

Python 使用MapLambda销毁小部件tkinter

Python 使用MapLambda销毁小部件tkinter,python,tkinter,lambda,Python,Tkinter,Lambda,我正在学习python的一些内置函数,并尝试使用map和lambda函数来销毁画布的子窗口小部件,但没有成功 以下是我尝试过的: from tkinter import * root = Tk() C = Canvas(root, bg='red',width=400,height=400) C.pack() Label(C,text='Label').pack() Button(C,text='Button').pack() map(lambda child: child.destr

我正在学习python的一些内置函数,并尝试使用map和lambda函数来销毁画布的子窗口小部件,但没有成功

以下是我尝试过的:

from tkinter import *

root = Tk()

C = Canvas(root, bg='red',width=400,height=400)
C.pack()

Label(C,text='Label').pack()

Button(C,text='Button').pack()

map(lambda child: child.destroy(), C.winfo_children())

root.mainloop()

map
返回一个iterable,传递给
map
的函数仅在iterable的项被使用/迭代时应用

您可以在地图iterable上使用
any()
/
list()
/
元组()
来使用其项目,以便应用该函数:

any(map(lambda child: child.destroy(), C.winfo_children()))
或者,如果您想要一行,只需使用列表理解:

[C.winfo_children()中child的child.destroy()]

谢谢,我已经做了列表理解,想看看我是否可以用map来做,在这种情况下,map的方式是真的更快了吗?只是计时一下,出于某种原因,列表理解似乎更快了