'hide frame on button press tkinter python

my arrow thing is just like a pointer. you can delete it if you add it to your code. It is not needed and will mess up your code if you don't delete it

I am using python 2.7.1 and Tkinter.

I have the code here:

# | this is where the show frame part is. |
# |---------------------------------------|
                   |
                   |__
                     |
def click_start():   |
        |____________|
        V
   f2.pack(after=f1, anchor=W, padx=5, pady=10)

f1 = Frame(root, width=10, height=20, bd=0, bg="#dcd9d3", pady=4, 
relief=FLAT).pack(side=TOP, anchor=W)

f2 = Frame(root, width=10, height=20, bd=0, bg="black", pady=4)

file_button = ttk.Button(f1, text="File", padding=3.5, width=3.5, 
command=click_start).pack(side=LEFT)

I now don't know how to hide the frame named f2. I want it so that when I press the file button, it will show the frame called f2 (I have done this part.)

Now, I need to hide it if I push the file button again.

Then I need to loop this function so that I can do this infinitely.



Solution 1:[1]

As you have used pack() on the frames, then you can use pack_forget() to remove the frame from the current pack manager. If you want to toggle the visibility of the frame, you can use winfo_manager() to check whether the frame is managed by any layout manager.

Just modify click_start() as below:

def click_start():
    f2.pack_forget() if f2.winfo_manager() else f2.pack(after=f1, anchor=W, padx=5, pady=10)

Solution 2:[2]

Use for Python 2 & 3

f2.pack_forget() # to hide
# if you use f2.pack() to show

also,

f2.grid_forget() # to hide
# if you use f2.grid() to show

f2.place_forget() # to hide
# if you use f2.place() to show

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 acw1668
Solution 2 Freddy Mcloughlan