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


Groups > comp.lang.python > #63858

Re: Tkinter GUI Error

From Peter Otten <__peter__@web.de>
Subject Re: Tkinter GUI Error
Date 2014-01-13 20:36 +0100
Organization None
References <1ab2aa93-1ba6-48b0-a5f4-5fb05cb523d1@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.5426.1389641835.18130.python-list@python.org> (permalink)

Show all headers | View raw


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 "<stdin>", line 1, in <module>
  File "<stdin>", 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.

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


Thread

Tkinter GUI Error fluttershy363@gmail.com - 2014-01-13 10:49 -0800
  Re: Tkinter GUI Error Lewis Wood <fluttershy363@gmail.com> - 2014-01-13 10:51 -0800
  Re: Tkinter GUI Error Christian Gollwitzer <auriocus@gmx.de> - 2014-01-13 20:03 +0100
    Re: Tkinter GUI Error Lewis Wood <fluttershy363@gmail.com> - 2014-01-13 11:21 -0800
  Re: Tkinter GUI Error Peter Otten <__peter__@web.de> - 2014-01-13 20:36 +0100
  Re: Tkinter GUI Error Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-01-13 20:39 -0500
  Re: Tkinter GUI Error Rick Johnson <rantingrickjohnson@gmail.com> - 2014-01-13 18:47 -0800
  Re: Tkinter GUI Error Chris Angelico <rosuav@gmail.com> - 2014-01-14 14:12 +1100
    Re: Tkinter GUI Error Lewis Wood <fluttershy363@gmail.com> - 2014-01-14 11:11 -0800
      Re: Tkinter GUI Error Lewis Wood <fluttershy363@gmail.com> - 2014-01-14 13:27 -0800
        Re: Tkinter GUI Error Christian Gollwitzer <auriocus@gmx.de> - 2014-01-14 22:33 +0100

csiph-web