Tkinter Sizegrip

Summary: in this tutorial, you’ll learn how to use the Tkinter Sizegrip widget that allows you to resize the entire application window.

Introduction to the Tkinter Sizegrip widget

The Sizegrip widget is typically located in the bottom-right corner of the window. It allows you to resize the entire application window:

To create a Sizegrip widget, you use the following syntax:

ttk.Sizegrip(master, **kw)Code language: Python (python)

In this syntax:

  • master: specify the parent widget where you want to place the Sizegrip. Typically, it is the main window.
  • **kw: specify the keyword arguments that configure the size grip’s appearance and behavior.

To make sure the Sizegrip widget works properly, you need to make the parent widget resizable.

If you use the main window e.g., root, you need to call the resizable() method with True values:

root.resizable(True, True)Code language: PHP (php)

Tkinter Sizegrip widget examples

Let’s take some examples of using the Sizegrip widget.

1) Using the Sizegrip widget with a grid layout

The following program creates and shows Sizegrip at the bottom-right corner of the main window using the grid layout:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.title('Sizegrip Demo')
root.geometry('300x200')
root.resizable(True, True)

# grid layout
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

# create the sizegrip
sizegrip = ttk.Sizegrip(root)
sizegrip.grid(row=1, sticky=tk.SE)


root.mainloop()Code language: Python (python)

Output:

Tkinter Sizegrip Widget Demo


How it works.

First, make sure the root window is resizable:

root.resizable(True, True)Code language: Python (python)

Second, configure the grid layout:

root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)Code language: Python (python)

Third, create a Sizegrip widget and place at the bottom-right corner of the window:

sg = ttk.Sizegrip(root)
sg.grid(row=1, sticky=tk.SE)Code language: Python (python)

2) Using the Sizegrip widget with the place layout

The following program demonstrates how to create a Sizegrip widget and place it at the bottom-right corner of the window using the place() method:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.resizable(True, True)

# create a sizegrip and place it at 
# the bottom-right corner of the window
sizegrip = ttk.Sizegrip(root)
sizegrip.place(relx=1, rely=1, anchor=tk.SE)

root.mainloop()Code language: Python (python)

In this example, we set the relx and rely parameters of the place() method to 1.0 to place the Sizegrip widget at the bottom-right corner of the main window. We also set the anchor parameter to tk.SE to place the south-east point of the Sizegrip to the specified coordinates.

3) Using the Sizegrip widget with the pack layout

The following program creates a Sizegrip widget and places it at the bottom-right corner using the pack() method:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.geometry('300x200')
root.resizable(True, True)


# Pack the Sizegrip at the 
# bottom-right corner of the window
sizegrip = ttk.Sizegrip(root)
sizegrip.pack(side="bottom", anchor=tk.SE)

root.mainloop()
Code language: Python (python)

In this example, we set the side parameter to “bottom,” and the anchor parameter to “se” to position the Sizegrip at the bottom-right corner of the window.

Summary

  • Use the Tkinter Sizegrip widget to allow users to resize the entire window application.
Did you find this tutorial helpful ?