Search code examples
pythonpython-3.xtkinter

tkinter resize frame and contents with main window


I am trying to work out how to control resizing of the window containing a frame. In the code I have three buttons across the top that should to stay exactly where they are.

When I drag the window to expand it, I want the frame and the text box it contains to expand with the master window. I have read that columnconfigure and rowconfigure can be used but not sure how to implement it.

from tkinter import *
from tkinter import scrolledtext

master_window = Tk()

# Create the buttons
btn_Image = Button(master_window, text='Image')
btn_Image.grid(row=1, column=1, padx=(10), pady=10, sticky=E + W)

btn_File = Button(master_window, text='File')
btn_File.grid(row=1, column=2, padx=(10), pady=10, sticky=E + W)

btn_Folder = Button(master_window, text='Folder')
btn_Folder.grid(row=1, column=3, padx=(10), pady=10, sticky=E + W)

# Group1 Frame ----------------------------------------------------
group1 = LabelFrame(master_window, text="Text Box", padx=5, pady=5)
group1.grid(row=2, column=1, columnspan=3, padx=10, pady=10, sticky=E+W+N+S)

# Create the textbox
txtbox = scrolledtext.ScrolledText(group1, width=40, height=10)
txtbox.grid(row=1, column=1,  columnspan=3, sticky=E+W+N+S)

mainloop()

Solution

  • The row and column numbers of the grid() layout manager start from 0, not from 1 (there is nothing wrong to start placing the elements wherever you want though, I just mention this because your code gives me the impression you think the cells start at 1, not at 0). Because I do not see the reason why to start placing the widgets at row=1 and column=1, in my solution below, I start placing at the 0 index.

    I suggest you to create a different container -parent widget- for the 3 buttons in questions (let us say a tkinter.Frame()).

    Here is the code:

    from tkinter import *
    from tkinter import scrolledtext
    
    master_window = Tk()
    
    # Parent widget for the buttons
    buttons_frame = Frame(master_window)
    buttons_frame.grid(row=0, column=0, sticky=W+E)    
    
    btn_Image = Button(buttons_frame, text='Image')
    btn_Image.grid(row=0, column=0, padx=(10), pady=10)
    
    btn_File = Button(buttons_frame, text='File')
    btn_File.grid(row=0, column=1, padx=(10), pady=10)
    
    btn_Folder = Button(buttons_frame, text='Folder')
    btn_Folder.grid(row=0, column=2, padx=(10), pady=10)
    
    # Group1 Frame ----------------------------------------------------
    group1 = LabelFrame(master_window, text="Text Box", padx=5, pady=5)
    group1.grid(row=1, column=0, columnspan=3, padx=10, pady=10, sticky=E+W+N+S)
    
    master_window.columnconfigure(0, weight=1)
    master_window.rowconfigure(1, weight=1)
    
    group1.rowconfigure(0, weight=1)
    group1.columnconfigure(0, weight=1)
    
    # Create the textbox
    txtbox = scrolledtext.ScrolledText(group1, width=40, height=10)
    txtbox.grid(row=0, column=0,   sticky=E+W+N+S)
    
    mainloop()
    

    Demo:

    At not stretched:

    enter image description here

    Average stretching:

    enter image description here

    After maximum stretching:

    enter image description here