from dialogtable import demos from quitter import quitter get base widget set button callback handlers attach quit object to me class demo(frame)def __init__(selfparent=none)frame __init__(selfparentself pack(label(selftext="basic demos"pack(for key in demosfunc (lambda key=keyself printit(key)button(selftext=keycommand=funcpack(side=topfill=bothquitter(selfpack(side=topfill=bothdef printit(selfname)print(name'returns =>'demos[name]()fetchcallprint if __name__ ='__main__'demo(mainloop(this script builds the same main button-bar windowbut notice that the callback handler is an anonymous function made with lambda nownot direct reference to dialog calls in the imported dialogtable dictionaryuse enclosing scope lookup func (lambda key=keyself printit(key)we talked about this in the prior tutorialbut this is the first time we've actually used lambda like thisso let' get the facts straight because button-press callbacks are run with no argumentsif we need to pass extra data to the handlerit must be wrapped in an object that remembers that extra data and passes it alongby deferring the call to the actual handler herea button press runs the function generated by the lambdaan indirect call layer that retains information from the enclosing scope the net effect is that the real handlerprintitreceives an extra required name argument giving the demo associated with the button pressedeven though this argument wasn' passed back from tkinter itself in effectthe lambda remembers and passes on state information noticethoughthat this lambda function' body references both self and key in the enclosing method' local scope in all recent pythonsthe reference to self just works because of the enclosing function scope lookup rulesbut we need to pass key in explicitly with default argument or else it will be the same in all the generated lambda functions--the value it has after the last loop iteration as we learned in enclosing scope references are resolved when the nested function is calledbut defaults are resolved when the nested function is created because self won' change after the function is madewe can rely on the scope lookup rules for that namebut not for loop variables like key dialogs