Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #49082
| 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 | <davea@davea.name> |
| 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 <davea@davea.name> |
| 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 | <b3d3518a-f24a-4c32-a41a-b99145753528@googlegroups.com> <kqa981$9uu$1@reader2.panix.com> |
| In-Reply-To | <kqa981$9uu$1@reader2.panix.com> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3767.1372106609.3114.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
On 06/24/2013 04:12 PM, John Gordon wrote:
> In <b3d3518a-f24a-4c32-a41a-b99145753528@googlegroups.com> 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("<Button-1>", 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) pablobarhamalzas@gmail.com - 2013-06-24 12:47 -0700
Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) pablobarhamalzas@gmail.com - 2013-06-24 12:53 -0700
Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) Peter Otten <__peter__@web.de> - 2013-06-24 22:05 +0200
Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-06-24 22:10 +0200
Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) John Gordon <gordon@panix.com> - 2013-06-24 20:12 +0000
Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-24 21:33 +0100
Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) Dave Angel <davea@davea.name> - 2013-06-24 16:43 -0400
Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) John Gordon <gordon@panix.com> - 2013-06-24 21:41 +0000
Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) pablobarhamalzas@gmail.com - 2013-06-24 13:19 -0700
Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-24 21:33 +0100
Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-24 21:19 +0100
csiph-web