What does sticky do in tkinter

Posted

in

by

In Tkinter, the sticky option is used to specify how a widget should expand to fill the space allotted to it within its container widget.

The sticky option is used when placing a widget using the grid geometry manager. When a widget is placed in a cell of the grid, it can be set to “stick” to one or more sides of the cell. The sticky option takes one or more of the following values:

  • N: stick to the top edge of the cell
  • S: stick to the bottom edge of the cell
  • E: stick to the right edge of the cell
  • W: stick to the left edge of the cell
  • NW: stick to the top-left corner of the cell
  • NE: stick to the top-right corner of the cell
  • SW: stick to the bottom-left corner of the cell
  • SE: stick to the bottom-right corner of the cell

For example, if you want a widget to fill the entire width of a cell and stick to the top edge of the cell, you can use:

widget.grid(row=0, column=0, columnspan=2, sticky='W' + 'E' + 'N')

This will place the widget in the first row, first column of the grid, and span two columns. The sticky option is set to 'W' + 'E' + 'N', which means the widget will stick to the left, right, and top edges of the cell, but not the bottom edge. As a result, the widget will expand horizontally to fill the width of the cell, but not vertically.

Example

import tkinter as tk

root = tk.Tk()

# Create a label widget and place it in the first row, first column of the grid
label = tk.Label(root, text="Hello, world!")
label.grid(row=0, column=0)

# Create a button widget and place it in the second row, first column of the grid
button = tk.Button(root, text="Click me!")
button.grid(row=1, column=0)

# Create an entry widget and place it in the second row, second column of the grid
entry = tk.Entry(root)
entry.grid(row=1, column=1, sticky='W')

# Create a text widget and place it in the third row, first column of the grid
text = tk.Text(root)
text.grid(row=2, column=0, columnspan=2, sticky='W'+'E'+'N'+'S')

branding_label = tk.Label(root, text="Powered by exasub.com")
branding_label.grid(row=3, column=0, columnspan=2, padx=10, pady=10, sticky='E')

root.mainloop()

In this example, we create a label widget, a button widget, an entry widget, and a text widget, and place them in a grid using the grid geometry manager.

The label widget is placed in the first row, first column of the grid, with no sticky option specified, so it will not expand to fill the cell.

The button widget is placed in the second row, first column of the grid, with no sticky option specified, so it will not expand to fill the cell.

The entry widget is placed in the second row, second column of the grid, with the sticky option set to ‘W’, so it will stick to the left edge of the cell and not expand to fill the cell.

The text widget is placed in the third row, first column of the grid, with the sticky option set to ‘W’+’E’+’N’+’S’, so it will stick to all four edges of the cell and expand to fill the entire cell.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *