Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

python - map() function does not seem to be doing what it is supposed to do

I'm trying to display a bunch of tkinter labels on the screen by storing the text and the other things like the value of padx and pady for that widget in a tuple nested within a list. I'm trying to iterate through each element of the list and display the label using the map() function. Here is a minimum example of my code

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()

But for some reason, map() does not seem to be working. My question is, why is that? Is it because I'm not using map() like I'm supposed to? If so, how do I correctly use it?

question from:https://stackoverflow.com/questions/65842713/map-function-does-not-seem-to-be-doing-what-it-is-supposed-to-do

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

map() will just return an iterator which need to be consumed in order to apply the function passed. Use something like list() or all() to consume it:

all(map(populate, lst))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...