Layout Issue with Tkinter GUI Design
I’m building my first Python GUI application using Tkinter and I’m having trouble making the layout responsive. My current design has two main sections but when I resize the window, the top area doesn’t expand properly.
What I want to achieve is keeping the bottom control panel fixed in size while allowing the upper display area to grow and fill the remaining space when the window is resized.
import tkinter as tk
main_window = tk.Tk()
# Upper display area
display_frame = tk.Frame(main_window)
display_frame.pack(side=tk.TOP, fill=tk.BOTH)
# Scrollbar for the list
vert_scrollbar = tk.Scrollbar(display_frame)
vert_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# Main list widget
data_listbox = tk.Listbox(display_frame)
data_listbox.pack(fill=tk.BOTH)
# Sample data
for num in range(50):
data_listbox.insert(tk.END, f"Item {num}")
# Connect scrollbar to listbox
data_listbox.config(yscrollcommand=vert_scrollbar.set)
vert_scrollbar.config(command=data_listbox.yview)
# Control panel at bottom
control_frame = tk.Frame(main_window)
control_frame.pack(side=tk.BOTTOM, fill=tk.X)
# Input fields section
input_section = tk.Frame(control_frame)
input_section.pack(side=tk.LEFT, fill=tk.X)
# Form labels and entries
tk.Label(input_section, text="Name").grid(row=0, column=0, sticky=tk.E)
tk.Label(input_section, text="Email").grid(row=0, column=2, sticky=tk.E)
tk.Label(input_section, text="Phone").grid(row=0, column=4, sticky=tk.E)
name_var = tk.StringVar()
email_var = tk.StringVar()
phone_var = tk.StringVar()
tk.Entry(input_section, textvariable=name_var).grid(row=0, column=1)
tk.Entry(input_section, textvariable=email_var).grid(row=0, column=3)
tk.Entry(input_section, textvariable=phone_var).grid(row=0, column=5)
# Button panel
button_panel = tk.Frame(control_frame)
button_panel.pack(side=tk.RIGHT)
# Action buttons
tk.Button(button_panel, text="Show All", width=10).grid(row=0, column=0)
tk.Button(button_panel, text="Find", width=10).grid(row=1, column=0)
tk.Button(button_panel, text="Insert", width=10).grid(row=2, column=0)
tk.Button(button_panel, text="Modify", width=10).grid(row=3, column=0)
tk.Button(button_panel, text="Remove", width=10).grid(row=4, column=0)
main_window.mainloop()
The current setup doesn’t expand the list area properly when I maximize the window. How can I make the top section responsive while keeping the bottom controls fixed?