← Back to Python Course | Chapter 14: Advanced Python & Tools | Lesson 13 of 15

Python tkinter Widgets

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

python
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

python
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

python
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

python
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

python
import tkinter
root = tkinter.Tk()
entry = tkinter.Entry(root)
entry.insert(0, "Alex")
entry.pack()
print(entry.get())

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.