Tkinter GUI Error
Christian Gollwitzer
auriocus at gmx.de
Mon Jan 13 14:03:42 EST 2014
Am 13.01.14 19:49, schrieb fluttershy363 at gmail.com:
>
> 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:
>
>
>
>
> from tkinter import *
> import random
> main = Tk()
> main.title("Crack the Code")
>
> def check1():
> entry = entry1var.get()
> if entry == num1:
> labelent1.destroy()
> labelent1 = Label(main, text="Correct!",fg="green").grid(row = 0, column = 3)
This is the wrong way to do it. Yes, in principle you could remove the
label and put a new one there; but it's much better to just change the
text of it by means of either
labelent1.configure(text="New text ")
or by linking a variable with the label variable at the setup time
somestringvar = StringVar("initial text")
Label(main, textvariable=somestringvar)
and then change that variable
somestringvar.set("New text")
Both of these don't solve the error, though; it has nothing to do with
Tk, you just did not make labelent1 global. However, I strongly advise
to use an object for the entire window, where you make this labelent1 an
instance variable (put into self).
Christian
More information about the Python-list
mailing list