0

This is a simplified version of my project. I want a button to use multiple times, but if I press it, it'll display the label text again. I don't know how to hide the label once the button is pressed again.

from tkinter import *


root = Tk()

root.geometry( "300x200" )



def show():
    szoveg = Label(root, text="hey")
    szoveg.pack()


button = Button(root, text= "fasz", command= show)
button.pack()


root.mainloop()

I tried text.pack_forget() in a different function, but it wasn't defined. I don't know how to define it in another function.

KT12
  • 549
  • 11
  • 24
niechill
  • 1
  • 1
  • Are you intending to create a _new_ label each time you press the button, or are you wanting to change the text on a label each time you press the button? – Bryan Oakley Aug 10 '23 at 14:47
  • You'd have to declare `szoveg` label in the global scope (i.e., outside the `show` function), then you can use `pack` and/or `pack_forget` on it inside a function without running into the `undefined` error – JRiggles Aug 10 '23 at 16:37
  • Does this answer your question? [In Tkinter is there any way to make a widget invisible?](https://stackoverflow.com/questions/3819354/in-tkinter-is-there-any-way-to-make-a-widget-invisible) – toyota Supra Aug 10 '23 at 18:58

1 Answers1

1

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

Quizzer515SY
  • 50
  • 1
  • 8
  • Thanks for answering, but this wasn't really my question. I'll explain it more in depth. So I'm familiar with basic programming principles, but I wanted to expand my knowledge so i watched a youtube tutorial, and the guy filming the tutorial said I should make a wheigt converter. And I did just that and it works. Exept for the fact that when I get the converted output under the button, (displayed with just a raw label,) and i try to convert again, i don't know how to make the previous output disappear. It looks like this: (entry) (button) output1 output2 I want output 1 to disappear. – niechill Aug 11 '23 at 14:43
  • @niechill Then you need to create the label once and update its text using `.config(text="...")` inside the function. – acw1668 Aug 11 '23 at 16:24