generally reference enclosing scope loop variables within nested functionfor examplebecause they will change as the loop progresses in most other casesthoughenclosing scope variables will take on only one value in their scope and so can be used freely we'll see this phenomenon at work in later examples that construct larger guis for nowremember that enclosing scopes are not complete replacement for defaultsdefaults are still required in some contexts to pass values into callback functions also keep in mind that classes are often better and simpler way to retain extra state for use in callback handlers than are nested functions because state is explicit in classesthese scope issues do not apply the next two sections cover this in detail bound method callback handlers let' get back to coding guis although functions and lambdas suffice in many casesbound methods of class instances work particularly well as callback handlers in guis-they record both an instance to send the event to and an associated method to call for instanceexample - shows examples - and - rewritten to register bound class method rather than function or lambda result example - pp \gui\intro\gui py import sys from tkinter import class helloclassdef __init__(self)widget button(nonetext='hello event world'command=self quitwidget pack(def quit(self)print('hello class method world'sys exit(self quit is bound method retains the self+quit pair helloclass(mainloop(on button presstkinter calls this class' quit method with no argumentsas usual but reallyit does receive one argument--the original self object--even though tkinter doesn' pass it explicitly because the self quit bound method retains both self and quitit' compatible with simple function callpython automatically passes the self argument along to the method function converselyregistering an unbound instance method that expects an argumentsuch as helloclass quitwon' workbecause there is no self object to pass along when the event later occurs adding user-defined callback handlers