Tkinter Frame

Tkinter Frame

In this tutorial, you’ll learn about the Tkinter Frame and how to manipulate its attributes, including sizes, paddings, and borders.

Introduction to Tkinter Frame Widget

A frame is a widget that displays as a simple rectangle. Typically, you use a frame to organize other widgets both visually and at the coding level.

To create a frame, you use the ttk.Frame class:

frame = ttk.Frame(master, **options)
Code copied!

Frame Attributes

A frame has various configuration options that determine its appearance:

  • borderwidth: Specify the border width of the frame. It defaults to zero.
  • class_: Set widget class name.
  • cursor: Change the cursor appearance when the mouse is over the frame.
  • height: Set the height of the frame.
  • padding: Create padding inside the frame and outside of the contained widgets.
  • relief: Specify the relief style for the border.
  • style: Specify custom widget style name.
  • takefocus: A boolean value that specifies whether the frame is visited during focus traversal. Defaults to False.
  • width: Set the width of the frame.

Frame Size

The size of a frame is determined by the size and layout of the widgets it contains. You can explicitly specify the height and width of the frame when creating it:

frame = ttk.Frame(master, height=200, width=300)
Code copied!

Padding

The padding allows you to add extra space inside the frame and outside of the contained widgets. Paddings are specified in pixels.

To specify padding for each side of the frame separately, you use the following:

frame['padding'] = (left, top, right, bottom)
Code copied!

For example:

frame['padding'] = (5, 10, 5, 10)
Code copied!

You can also specify paddings for the left, right, and top, bottom as follows:

frame['padding'] = (5, 10)
Code copied!

Frame Borders

By default, the border width of a frame is zero. To set a border for a frame, you need to set both the border’s width and style:

frame['borderwidth'] = 5
frame['relief'] = 'sunken'
Code copied!

Example: Creating a Replace Window

We’re going to create a Replace window that is common in text editors:

  • The left frame consists of Label, Entry, and Checkbox widgets.
  • The right frame consists of Button widgets.

The following program illustrates how to create the Replace window:

import tkinter as tk
from tkinter import ttk

def create_input_frame(container):
    frame = ttk.Frame(container)
    frame.columnconfigure(0, weight=1)

    ttk.Label(frame, text='Find what:').grid(column=0, row=0, sticky=tk.W)
    entry = ttk.Entry(frame, width=30)
    entry.focus()
    entry.grid(column=1, row=0, sticky=tk.W)

    ttk.Label(frame, text='Replace with:').grid(column=0, row=1, sticky=tk.W)
    replacement_entry = ttk.Entry(frame, width=30)
    replacement_entry.grid(column=1, row=1, sticky=tk.W)

    ttk.Checkbutton(frame, text='Match case').grid(column=0, row=2, sticky=tk.W)
    ttk.Checkbutton(frame, text='Wrap around').grid(column=0, row=3, sticky=tk.W)

    for widget in frame.winfo_children():
        widget.grid(padx=5, pady=5)

    return frame

def create_button_frame(container):
    frame = ttk.Frame(container)
    ttk.Button(frame, text='Find Next').grid(column=0, row=0)
    ttk.Button(frame, text='Replace').grid(column=0, row=1)
    ttk.Button(frame, text='Replace All').grid(column=0, row=2)
    ttk.Button(frame, text='Cancel').grid(column=0, row=3)

    for widget in frame.winfo_children():
        widget.grid(padx=5, pady=5)

    return frame

def create_main_window():
    root = tk.Tk()
    root.title('Replace')
    root.resizable(0, 0)

    input_frame = create_input_frame(root)
    input_frame.grid(column=0, row=0)

    button_frame = create_button_frame(root)
    button_frame.grid(column=1, row=0)

    root.mainloop()

if __name__ == "__main__":
    create_main_window()
Code copied!

Conclusion

A ttk.Frame is a simple rectangle widget that can hold other widgets. Tkinter frames are used to organize user interfaces visually and at the coding level.

Key takeaways from this tutorial:

  • Understand the various attributes of the Tkinter Frame.
  • Learn how to set padding and borders effectively.
  • See a practical example of using frames to create a user interface.