我试图通过将文本和小部件的padx和pady的值存储在嵌套在列表中的元组中来在屏幕上显示一堆tkinter标签。我尝试遍历列表中的每个元素,并使用map()函数显示标签。以下是我的代码的最小示例
import tkinter as tk
import tkinter.ttk as ttk
from ttkthemes import ThemedTk
def func() :
print("in func")
def populate(tple) :
print(f"In populate with tuple {tple}")
label = ttk.Label(frame,text=tple[0],style=tple[1])
label.grid(row=lst.index(tple),column=0,padx=tple[2],pady=tple[3],sticky=tple[4])
frame.update_idletasks()
tk.Grid.columnconfigure(frame, 0, weight=1)
lst = [("This is text1","style1.TLabel",40,15,None),
("This is text2","style2.TLabel",20,15,tk.S)]
map(populate, lst)
frame = ThemedTk(theme="black")
frame.config(bg="#424242")
style = ttk.Styles()
func()
frame.mainloop()但由于某些原因,map()似乎不起作用。我的问题是,为什么会这样?是不是因为我没有按照我应该使用的方式使用map()?如果是这样,我如何正确使用它?
发布于 2021-01-22 18:02:17
map()将只返回一个迭代器,为了应用传递的函数,需要使用该迭代器。使用像list()或all()这样的东西来使用它:
all(map(populate, lst))https://stackoverflow.com/questions/65842713
复制相似问题