Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #49072
| Date | 2013-06-24 22:10 +0200 |
|---|---|
| From | Antoon Pardon <antoon.pardon@rece.vub.ac.be> |
| Subject | Re: What's wrong with this code? (UnboundLocalError: local variable referenced before assignment) |
| References | <b3d3518a-f24a-4c32-a41a-b99145753528@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3759.1372104684.3114.python-list@python.org> (permalink) |
Op 24-06-13 21:47, pablobarhamalzas@gmail.com schreef:
> Hi there! I'm quite new to programming, even newer in python (this is actually the first thing I try on it), and every other topic I've seen on forums about my problem doesn't seem to help.
>
> So, the following lines are intended to draw a white square (which it does), turn it to blue when you click on it, and back to white when you click on it again (and so on). Here's what I wrote (python 3 syntax):
>
>
> from tkinter import *
>
> root = Tk()
> root.geometry("500x500")
>
> w = Canvas(root, width=500, height=500)
> w.pack()
>
> coords = (x1, y1, x2, y2) = (100, 100, 200, 200)
>
> rect = w.create_rectangle(coords, fill="white")
> 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.
No it is not.
In Python, when you assign to a variable within a function, that
variable will be treated as a local variable. If you have a global
variable with the same name, that global variable will just for
the duration of the function become inaccessible.
The quick solution in this case is to include a global statement.
Something like
def change(event)
global isWhite
...
--
Antoon Pardon
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