Home > database >  How to actually add buttons and other stuff within different frames in Tkinter
How to actually add buttons and other stuff within different frames in Tkinter

Time:03-20

I'm just learning how to make a GUI with tkinter. There seems to be lots of resources on how to make new frames, but I cannot seem to find anything that explains how to actually add stuff within multiple frames without encountering problems.

I've got a Notebook as I'm using multiple tabs to view the 2 different frames I am using. Dead, simple, 2 buttons in the first frame, one hides the second tab and the other button shows the second tab. Within the second tab is the second frame. All works fine, but as soon as I put anything inside the second frame, both frames shrink down. Code is below:

from tkinter import *

from tkinter import ttk

root = Tk()
root.title('Tabs')
root.geometry("500x500")  

my_notebook = ttk.Notebook(root)
my_notebook.pack(pady=15)

def hide():
    my_notebook.hide(1)
    
def show():
    my_notebook.add(my_frame2, text="Red Tab")

my_frame1= Frame(my_notebook, width=500, height=500, bg="blue")
my_frame2= Frame(my_notebook, width=500, height=500, bg="red")

my_frame1.pack(fill="both", expand=1)
my_frame2.pack(fill="both", expand=1)


my_notebook.add(my_frame1, text="Blue Tab")
my_notebook.add(my_frame2, text="Red Tab")
    

my_button = Button(my_frame1, text="Hide Tab2", command=hide).grid(row=0, column=0)
my_button2 = Button(my_frame1, text="Show Tab2", command=show).grid(row=1, column=1)

my_button3 = Button(my_frame2, text="Random button").grid(row=1, column=1)

root.mainloop()

When my_button3 is removed, everything looks fine. When it is in the code everything shrinks down. Does anyone know what I am doing that is causing this?

CodePudding user response:

In tkinter, when a frame has no children, it occupies the entire space it is initialised with. However, when you add some child widgets to it, it takes only the required space to accommodate its children.

In your code, when you add my_button3 to my_frame2, it shrinks to accommodate the button.

Earlier, when you didn't add the button to the second frame, the notebook was properly sized even though my_frame1 didn't require the entire space because my_frame2 was still empty. my_frame2 decided the size of the notebook.

So, the direct solution to your problem would be:

my_frame1.grid_propagate(False)

OR

my_frame2.grid_propagate(False)

What does grid_propagate do?

It basically tells the frame to propagate/not propagate depending on its children. (True = propagate [default])

Similar to grid_propagate, there is also pack_propagate. The right method to use depends on the geometry manager used to add the children to the frame. (not the geometry manager used to add the frame to its parent)

However, using grid_propagate and pack_propagate is generally not advisable because you are hardcoding the space management. Tkinter is good at resizing a parent according to its child widgets. On most occasions, this makes the entire GUI look good with the right proportions.

  • Related