Python tkinter Widgets
In this page:
Adding Labels
The Label widget displays static text (or an image) on the window and is typically the simplest widget to start with, since it has no interactivity of its own -- just content that's shown to the user.
Example: Adding Labels
import tkinter
root = tkinter.Tk()
label = tkinter.Label(root, text="Hello")
label.pack()
print(label["text"])
Adding Action Buttons
The Button widget renders a clickable button, and passing a function to its command parameter wires that function to run whenever the button is clicked -- this callback pattern is how most interactivity in a Tkinter app gets triggered.
Example: Adding Action Buttons
import tkinter
def on_click():
print("Button clicked")
root = tkinter.Tk()
button = tkinter.Button(root, text="Click me", command=on_click)
button.pack()
button.invoke()
Placing Widgets with pack
The pack() geometry manager places widgets into the window one after another, stacking them vertically by default (or side-by-side with side=left), automatically sizing itself around the widgets it contains without needing exact coordinates.
Example: Placing Widgets with pack
import tkinter
root = tkinter.Tk()
tkinter.Label(root, text="One").pack()
tkinter.Label(root, text="Two").pack(side="left")
print("Widgets stacked with pack()")
Positioning with grid
The grid() geometry manager arranges widgets into a row-and-column layout much like a spreadsheet, giving finer control over precise widget positioning than pack()'s simpler stacking behavior -- though mixing pack() and grid() within the same container causes errors, so a window should stick to one or the other.
Example: Positioning with grid
import tkinter
root = tkinter.Tk()
tkinter.Label(root, text="Row 0").grid(row=0, column=0)
tkinter.Label(root, text="Row 1").grid(row=1, column=0)
print("Widgets arranged with grid()")
Input Fields with Entry
The Entry widget provides a single-line text input box where a user can type, and calling .get() on it retrieves whatever text is currently entered -- the standard way to read user input from a Tkinter form.
Example: Input Fields with Entry
import tkinter
root = tkinter.Tk()
entry = tkinter.Entry(root)
entry.insert(0, "Alex")
entry.pack()
print(entry.get())
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- Python PEP 8 Style Guide
- Python Debugging Techniques
- Python Testing with unittest
- Python Common Mistakes
- Python Interview Questions
- Python map() & filter()
- Python reduce()
- Python zip() & enumerate()
- Python sorted() & key Functions
- Python Comprehensions Advanced
- Python Turtle Graphics
- Python tkinter Introduction
- Python tkinter Widgets
- Python pygame Introduction
- Python Mini Projects