Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #49074

Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment)

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nntp.club.cc.cmu.edu!micro-heart-of-gold.mit.edu!bloom-beacon.mit.edu!panix!gordon
From John Gordon <gordon@panix.com>
Newsgroups comp.lang.python
Subject Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment)
Date Mon, 24 Jun 2013 20:12:49 +0000 (UTC)
Organization PANIX Public Access Internet and UNIX, NYC
Lines 38
Message-ID <kqa981$9uu$1@reader2.panix.com> (permalink)
References <b3d3518a-f24a-4c32-a41a-b99145753528@googlegroups.com>
NNTP-Posting-Host panix1.panix.com
X-Trace reader2.panix.com 1372104769 10206 166.84.1.1 (24 Jun 2013 20:12:49 GMT)
X-Complaints-To abuse@panix.com
NNTP-Posting-Date Mon, 24 Jun 2013 20:12:49 +0000 (UTC)
User-Agent nn/6.7.3
Xref csiph.com comp.lang.python:49074

Show key headers only | View raw


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.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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