[email protected] 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.
--
https://mail.python.org/mailman/listinfo/python-list