Demonstrating Tkinter pack and pack_forget

TrustyTony 1 Tallied Votes 12K Views Share

Also demonstrate changing the text of the widgets label dynamically.

from Tkinter import *

root = Tk()

button = Button(text="Push me", command=toggle)
button.pack()

label = Label(text="Hello")
label.pack()

def toggle():
    if label.winfo_ismapped():
        button['text']='unmap'
        label.pack_forget()
    else:
        button['text']='map'
        label.pack()

root.wait_window()
TrustyTony 888 pyMod Team Colleague Featured Poster

def should go in beginning, sorry.

from Tkinter import *
def toggle():
    if label.winfo_ismapped():
        button['text']='unmap'
        label.pack_forget()
    else:
        button['text']='map'
        label.pack()

root = Tk()

button = Button(text="Push me", command=toggle)
button.pack()

label = Label(text="Hello")
label.pack()

root.wait_window()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.