Python tkinter Widgets
In this page:
widget = tk.Button(root, text="text", command=function_name)
widget.pack()
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") # displays static text, no interactivity
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) # on_click runs when clicked
button.pack()
button.invoke() # simulates a click for this example
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() # stacked vertically by default
tkinter.Label(root, text="Two").pack(side="left") # placed to the left instead
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) # row/column layout, like a spreadsheet
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) # single-line text input box
entry.insert(0, "Alex")
entry.pack()
print(entry.get()) # reads the current text in the box
- Mixing
pack()andgrid()in the same container, which can hang the program or raise an error. - Writing
command=func()instead ofcommand=func, which runs the function immediately instead of on click. - Writing
label = Label(root).pack(), which storesNoneinlabelbecausepack()returns nothing; create the widget on one line and pack it on the next.
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