Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.134.4.91.MISMATCH!news2.euro.net!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'else:': 0.03; 'root': 0.05; '(python': 0.07; 'assign': 0.07; 'subject:code': 0.07; 'tkinter': 0.07; '"if': 0.09; 'blue': 0.09; 'function,': 0.09; 'referenced': 0.09; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; '(100,': 0.16; '(x1,': 0.16; 'before.': 0.16; 'it),': 0.16; 'received:192.168.1.4': 0.16; 'statement.': 0.16; 'subject: \n ': 0.16; 'subject:variable': 0.16; 'there!': 0.16; 'tk()': 0.16; 'variable.': 0.16; 'x2,': 0.16; 'y1,': 0.16; 'variable': 0.18; 'help.': 0.21; 'import': 0.22; 'header:User- Agent:1': 0.23; 'error': 0.23; "i've": 0.25; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; '(this': 0.29; 'topic': 0.29; "doesn't": 0.30; 'newer': 0.30; "i'm": 0.30; '(which': 0.31; 'lines': 0.31; 'occurs': 0.31; 'quite': 0.32; 'not.': 0.33; 'problem': 0.35; 'subject:with': 0.35; 'something': 0.35; 'false': 0.36; 'programming,': 0.36; 'so,': 0.37; 'turn': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'even': 0.60; 'skip:u 10': 0.60; 'duration': 0.60; 'new': 0.61; 'first': 0.61; 'back': 0.62; 'email addr:gmail.com': 0.63; 'become': 0.64; 'within': 0.65; 'forums': 0.68; 'clicking': 0.73; 'square': 0.74; 'click': 0.77; 'subject:this': 0.83; 'pardon': 0.84; 'received:195.238': 0.84; 'received:195.238.6': 0.84; 'received:belgacom.be': 0.84; 'received:isp.belgacom.be': 0.84; 'subject:before': 0.84 X-Belgacom-Dynamic: yes X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AjcDALymyFFbtrj4/2dsb2JhbAANTcNCAwGBIIMYAQEEOEARCyEWDwkDAgECAQ82EwYCAheHYQOoWIlMDUuIB4JVihOBJGRmFoNNA5VcgWeMIIZ8gTo Date: Mon, 24 Jun 2013 22:10:15 +0200 From: Antoon Pardon User-Agent: Mozilla/5.0 (X11; Linux i686; rv:10.0.12) Gecko/20130116 Icedove/10.0.12 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-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: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1372104684 news.xs4all.nl 15903 [2001:888:2000:d::a6]:60790 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:49072 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("", 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