Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'argument': 0.05; 'assign': 0.07; 'attribute': 0.07; 'subject:code': 0.07; 'variables': 0.07; '"if': 0.09; 'arguments': 0.09; 'attributes': 0.09; 'executed': 0.09; 'referenced': 0.09; 'toggle': 0.09; 'def': 0.12; 'before.': 0.16; 'for,': 0.16; 'local.': 0.16; 'subject: \n ': 0.16; 'subject:variable': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'all,': 0.19; 'bit': 0.19; 'normally': 0.19; 'not,': 0.20; '(in': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'decide': 0.24; 'possibly': 0.26; 'pass': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'generally': 0.29; "doesn't": 0.30; 'especially': 0.30; 'lines': 0.31; 'that.': 0.31; 'occurs': 0.31; 'writes:': 0.31; 'could': 0.34; 'problem': 0.35; 'subject:with': 0.35; 'case,': 0.35; 'but': 0.35; 'there': 0.35; 'false': 0.36; 'object,': 0.36; 'programming,': 0.36; 'method': 0.36; "i'll": 0.36; 'possible': 0.36; 'should': 0.36; 'operating': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'pm,': 0.38; 'that,': 0.38; 'to:addr:python.org': 0.39; 'called': 0.40; 'skip:u 10': 0.60; 'new': 0.61; 'john': 0.61; "you're": 0.61; 'email addr:gmail.com': 0.63; 'name': 0.63; 'such': 0.63; 'needing': 0.65; 'within': 0.65; 'design.': 0.68; 'improvement.': 0.68; 'received:74.208': 0.68; 'clicking': 0.73; 'subject:this': 0.83; 'received:74.208.4.194': 0.84; 'subject:before': 0.84; 'tricky': 0.84; 'boxes,': 0.91 Date: Mon, 24 Jun 2013 16:43:15 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130510 Thunderbird/17.0.6 MIME-Version: 1.0 To: python-list@python.org Subject: Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:OY0ZkzFtQSE+T7GQBFHinIHWmi7lz+9f0sABf4gfyJU NU9BUYRIx0BxPsmwfWRJiiR9VmcSShkyA6h1jyGyGyzamC+zFu Xssyce9XzBk8XFhnOgJu/pXPi/FdSqoYHM5iYyMTeu0tzEotpN PK+1mg3dpyYpobi8nrftagiCdN3LZG7ZsO0/DVWTueI34sUbzZ K+YBEsUJZyb0Gi3P358CIeLXnsvEnDg1rJOUaszPIntfNVi73a D2JFNiBQNU4kUy875Tk2LZR2Rowhde8uU2rCRhXHu8XlsT8bAU 2g0X/IrSFEVGLX+O4F5zrXScqJlOMKnJkrMc+wlstyENIa8LNv LrXv8LHOKb06VdTs9QnQ= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 68 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1372106609 news.xs4all.nl 15896 [2001:888:2000:d::a6]:33361 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:49082 On 06/24/2013 04:12 PM, John Gordon wrote: > In pablobarhamalzas@gmail.com writes: > >> isWhite = True >> >> def change(event): >> if event.x > x1 and event.x < x2 and event.y > y1 and event.y < y2: >> if isWhite: >> w.itemconfig(rect, fill="blue") >> isWhite = False >> else: >> w.itemconfig(rect, fill="white") >> isWhite = True >> >> w.bind("", change) >> >> root.mainloop() > >> The problem occurs when clicking on the white square. The following error >> appears: >> "if isWhite: >> UnboundLocalError: local variable 'isWhite' referenced before assignment" > >> However, the isWhite variable is clearly defined at "True" a few lines >> before. > > Since you're new to programming, this might be a bit tricky to explain, > but I'll do my best. :-) > > The problem is that change() isn't being executed here; instead it's being > executed from within root.mainloop(), whenever the user presses button-1. > > And within root.mainloop(), there is no variable called isWhite. > Actually that's irrelevant. Whether or not there's one global with the same name, or twenty-three object attributes with the same name, the fact that there's a binding of the local makes that name a local. The only way to avoid that is not to bind, or to use global or nonlocal declarations. Pablo: Global variables are generally frowned upon, unless they're constant. If they're constant, use ALLCAPS to indicate that. Since this is not, it would normally be an attribute of some object, in your case, possibly the object w. And of course, w should have been an argument to the function as well, since you're operating on it. But you may be stuck with that, because of tkinter's design. Anyway, you can assign w.isWhite = True and access if w.isWhite with impunity, since w is not being bound inside the function. When you need to pass extra arguments that the event model doesn't allow for, one approach is to use functools.partial(). And it's also possible that there's a method (in tkinter) on event that let's you find the object that it's acting upon, w. In this case, you could avoid needing a global at all, which would be a big improvement. Especially when you decide to have multiple such boxes, and want each to be able to toggle colors independently. -- DaveA