Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'elif': 0.05; 'assign': 0.07; 'assignment': 0.07; 'column': 0.07; 'modify': 0.07; 'subject:Error': 0.07; 'tkinter': 0.07; "'a'": 0.09; 'callback': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'referenced': 0.09; 'way:': 0.09; 'python': 0.11; 'def': 0.12; 'gui': 0.12; '"global"': 0.16; 'column=3)': 0.16; 'declarations': 0.16; 'defined.': 0.16; 'determines': 0.16; 'effect.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:GUI': 0.16; 'subject:Tkinter': 0.16; 'tk()': 0.16; 'exception': 0.16; 'fix': 0.17; 'thanks,': 0.17; 'wrote:': 0.18; 'variable': 0.18; 'module': 0.19; 'trying': 0.19; 'replacing': 0.19; '>>>': 0.22; 'example': 0.22; 'import': 0.22; 'separate': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'replace': 0.24; 'skip:l 30': 0.24; 'text.': 0.24; '15,': 0.26; 'references': 0.26; 'skip:" 30': 0.26; 'post': 0.26; 'code:': 0.26; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; 'label': 0.30; 'statement': 0.30; 'code': 0.31; 'easier': 0.31; '"",': 0.31; 'produces': 0.31; 'reduced': 0.31; 'file': 0.32; 'text': 0.33; '(most': 0.33; 'minimal': 0.33; 'problem': 0.35; '(2)': 0.35; 'test': 0.35; 'instances': 0.36; 'shorter': 0.36; 'skip:" 50': 0.36; 'entry': 0.36; 'possible': 0.36; 'should': 0.36; 'level': 0.37; 'to:addr:python-list': 0.38; 'recent': 0.39; 'delete': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'tell': 0.60; 'skip:t 30': 0.61; 'full': 0.61; 'email addr:gmail.com': 0.63; 'name': 0.63; 'more': 0.64; 'here': 0.66; 'clicking': 0.73; "'local'": 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Tkinter GUI Error Date: Mon, 13 Jan 2014 20:36:53 +0100 Organization: None References: <1ab2aa93-1ba6-48b0-a5f4-5fb05cb523d1@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50848b44.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 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: 114 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389641835 news.xs4all.nl 2954 [2001:888:2000:d::a6]:60782 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:63858 fluttershy363@gmail.com wrote: > Inside the function is where I am having the problem, I am trying to get > it to delete the label so that it may then replace it with a shorter text. > Here is the full code: > def check1(): > entry = entry1var.get() > if entry == num1: > labelent1.destroy() > labelent1 = Label(main, text="Correct!",fg="green").grid(row = 0, > column = 3) > elif entry > num1: > labelent1.destroy() > labelent1 = Label(main, text="Too Big",fg="red").grid(row = 0, > column = 3) > elif entry < num1: > labelent1.destroy() > labelent1 = Label(main, text="Too Small",fg="red").grid(row = 0, > column = 3) > And this is the error displayed when clicking on button1: > > Exception in Tkinter callback > Traceback (most recent call last): > File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__ > return self.func(*args) > File "C:/Users/User/Desktop/Programming/Tkinter/Tkinter.py", line 15, in > check1 > labelent1.destroy() > UnboundLocalError: local variable 'labelent1' referenced before assignment > > > Thanks, Lewis. Kudos, your problem description is very clear! Your post would be perfect, had you reduced the number of Labels from three to one ;) The error you are seeing has nothing to do with the GUI. When you assign to a name inside a function Python determines that the name is local to that function. A minimal example that produces the same error is >>> a = "global" >>> def test(): ... print(a) ... a = "local" ... >>> test() Traceback (most recent call last): File "", line 1, in File "", line 2, in test UnboundLocalError: local variable 'a' referenced before assignment The name `a` passed to print() references the local `a` which is not yet defined. A possible fix is to tell Python to reference the global `a` >>> a = "global" >>> def test(): ... global a ... print(a) ... a = "local" ... >>> test() global >>> a 'local' However, in the case of your GUI code you should not destroy and create Label instances -- it is more efficient (and easier I think) to modify the Label's text: (1) working demo with 'global' -- don't do it that way: from tkinter import * main = Tk() def check1(): global labelent1 labelent1.destroy() labelent1 = Label(main, text="Correct!", fg="green") labelent1.grid(row = 0, column = 3) # must be a separate statement as # grid() returns None Button(main, text="Try Number", command=check1).grid(row=0, column=2) labelent1 = Label(main, text="Waiting for Input") labelent1.grid(row=0, column=3) # must be a separate statement as # grid() returns None main.mainloop() (2) The way to go, modify the label text instead of replacing it: from tkinter import * main = Tk() def check1(): labelent1.configure(text="Correct!", fg="green") Button(main, text="Try Number", command=check1).grid(row=0, column=2) labelent1 = Label(main, text="Waiting for Input") labelent1.grid(row=0, column=3) main.mainloop() > global num1 By the way, global declarations on the module level have no effect.