The question was a little hard to understand, but it sounds like what you want is the button to toggle the Label on and off. This can be accomplished in a few different ways, depending on whether you want the label to be declared globally or not.
One way of doing this is shown here:
from tkinter import *
root = Tk()
root.geometry( "300x200" )
def toggle():
try:
# Check to see if the root window has a child named "szoveg", and if so destroy it
root.nametowidget('.szoveg').destroy()
except:
# Otherwise, create a label named "szoveg"
szoveg = Label(root, text="hey", name="szoveg")
szoveg.pack()
button = Button(root, text= "button", command= toggle)
button.pack()
root.mainloop()
Every time you click the button, there is a check to see if the widget "szoveg" exists as a child of the root window. If there is one, remove it. If the try
results in an error (aka there is no label named "szoveg"), then it will move on the the except
, which creates the label.
Another way of doing this is just having the label created outside of the callback function, like so:
from tkinter import *
root = Tk()
root.geometry( "300x200" )
szoveg = Label(root, text="hey", name="szoveg")
def toggle():
if szoveg.winfo_ismapped():
szoveg.pack_forget()
else:
szoveg.pack()
button = Button(root, text= "button", command= toggle)
button.pack()
root.mainloop()
szoveg.winfo_ismapped()
checks to see if the pack
or grid
methods have been called on it.
A handy thing about this method is that the label widget always exists, so if you need to do something else with it there won't be any errors.
Hope this helped,
Quizzer515SY