Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to set the sticky button property properly?
Tkinter Buttons can be configured through different available attributes and properties. The sticky property makes a widget "stick" to specific sides of its grid cell, controlling alignment and expansion behavior. The sticky property accepts compass directions: N (North), E (East), S (South), W (West), and combinations like NE, NW, SE, SW.
Basic Sticky Property Usage
Here's how to use the sticky property with different directional values ?
# Import the tkinter library
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame
win = Tk()
win.geometry("750x300")
win.title("Sticky Button Example")
# Configure grid weights for better demonstration
win.columnconfigure(0, weight=1)
win.columnconfigure(1, weight=1)
win.columnconfigure(2, weight=1)
win.rowconfigure(0, weight=1)
win.rowconfigure(1, weight=1)
win.rowconfigure(2, weight=1)
# Create buttons with different sticky properties
btn1 = ttk.Button(win, text="Sticky North")
btn1.grid(row=0, column=0, sticky="N", padx=10, pady=10)
btn2 = ttk.Button(win, text="Sticky South")
btn2.grid(row=0, column=1, sticky="S", padx=10, pady=10)
btn3 = ttk.Button(win, text="Sticky East")
btn3.grid(row=0, column=2, sticky="E", padx=10, pady=10)
btn4 = ttk.Button(win, text="Sticky West")
btn4.grid(row=1, column=0, sticky="W", padx=10, pady=10)
btn5 = ttk.Button(win, text="Sticky North-East")
btn5.grid(row=1, column=1, sticky="NE", padx=10, pady=10)
btn6 = ttk.Button(win, text="Sticky South-West")
btn6.grid(row=1, column=2, sticky="SW", padx=10, pady=10)
win.mainloop()
Sticky with Expansion
Using sticky="EW" makes the widget stretch horizontally, while sticky="NS" stretches vertically ?
from tkinter import *
from tkinter import ttk
win = Tk()
win.geometry("600x400")
win.title("Sticky Expansion Example")
# Configure grid weights
win.columnconfigure(0, weight=1)
win.columnconfigure(1, weight=1)
win.rowconfigure(0, weight=1)
win.rowconfigure(1, weight=1)
win.rowconfigure(2, weight=1)
# Button that stretches horizontally
btn1 = ttk.Button(win, text="Stretch Horizontally (EW)")
btn1.grid(row=0, column=0, columnspan=2, sticky="EW", padx=10, pady=5)
# Button that stretches vertically
btn2 = ttk.Button(win, text="Stretch Vertically (NS)")
btn2.grid(row=1, column=0, sticky="NS", padx=10, pady=5)
# Button that stretches in all directions
btn3 = ttk.Button(win, text="Stretch All Directions (NSEW)")
btn3.grid(row=1, column=1, rowspan=2, sticky="NSEW", padx=10, pady=5)
# Normal button for comparison
btn4 = ttk.Button(win, text="No Sticky")
btn4.grid(row=2, column=0, padx=10, pady=5)
win.mainloop()
Sticky Property Values
| Sticky Value | Alignment | Behavior |
|---|---|---|
| N | North (Top) | Widget sticks to top of cell |
| S | South (Bottom) | Widget sticks to bottom of cell |
| E | East (Right) | Widget sticks to right of cell |
| W | West (Left) | Widget sticks to left of cell |
| EW | East-West | Widget stretches horizontally |
| NS | North-South | Widget stretches vertically |
| NSEW | All directions | Widget fills entire cell |
Conclusion
The sticky property controls widget alignment and expansion within grid cells. Use directional values (N, S, E, W) for positioning, and combinations like EW or NSEW for stretching behavior. Always configure grid weights with columnconfigure() and rowconfigure() for proper expansion effects.
