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

Python tkinter Widgets

Widgets are the pieces you put in a window, like labels, buttons, and text boxes, like LEGO bricks for an app. You arrange them where you want them.
Syntax
python
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

python
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

python
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

python
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

python
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

python
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
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Mixing pack() and grid() in the same container, which can hang the program or raise an error.
  2. Writing command=func() instead of command=func, which runs the function immediately instead of on click.
  3. Writing label = Label(root).pack(), which stores None in label because pack() returns nothing; create the widget on one line and pack it on the next.

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.