What is tkinter?
Tkinter is Python’s standard GUI (Graphical User Interface) library.
Built-in: You don’t need to install it separately if you have the default Python distribution - it ships with Python.
Wrapper: It’s a thin object-oriented wrapper around Tcl/Tk, which is an older but very stable GUI toolkit.
Purpose: Lets you build desktop applications with windows, buttons, labels, menus, text boxes, etc.
Think of it as Python’s way to make actual windows instead of just printing to the terminal.
Start first window
app.py
import tkinter as tk
# Create the main application window
root = tk.Tk()
# Set window title
root.title("My First Tkinter Window")
# Set window size (width x height)
root.geometry("400x300")
# Start the Tkinter event loop
root.mainloop()
Open a terminal and run:
python app.py
You'll see the first little white window.
Note: I am using Python 3.12 with Tcl-tk@8
Install Tcl-tk if not yet ready on your PC.
This is how you can install it on MacOS (I am using Macbook M1).
brew uninstall tcl-tk
brew install tcl-tk@8
Install Python 3.12
I am using pyenv to manage Python version, you can install it by this command:
brew install pyenv
Now, install Python 3.12 with Tcl-tk
env \
PATH="/opt/homebrew/opt/tcl-tk@8/bin:$PATH" \
LDFLAGS="-L/opt/homebrew/opt/tcl-tk@8/lib" \
CPPFLAGS="-I/opt/homebrew/opt/tcl-tk@8/include" \
PKG_CONFIG_PATH="/opt/homebrew/opt/tcl-tk@8/lib/pkgconfig" \
PYTHON_CONFIGURE_OPTS="--with-tcltk-includes='-I/opt/homebrew/opt/tcl-tk@8/include' --with-tcltk-libs='-L/opt/homebrew/opt/tcl-tk@8/lib -ltcl8.6 -ltk8.6'" \
pyenv install 3.12.8
Add first widget
import tkinter as tk
# Create main window
root = tk.Tk()
root.title("My First Tkinter App")
root.geometry("400x250")
# Add a label
label = tk.Label(root, text="Enter your name:", font=("Arial", 12))
label.pack(pady=10)
# Add a text entry box
entry = tk.Entry(root, font=("Arial", 12))
entry.pack(pady=5)
# Add a button
def say_hello():
name = entry.get()
if name.strip():
output_label.config(text=f"Hello, {name}!")
else:
output_label.config(text="Please type your name.")
button = tk.Button(root, text="Submit", font=("Arial", 12), command=say_hello)
button.pack(pady=10)
# Output label
output_label = tk.Label(root, text="", font=("Arial", 12), fg="blue")
output_label.pack(pady=10)
# Run the application
root.mainloop()
And here is our output:
What are tkinter widgets?
In Tkinter, a widget is just a GUI element — the building blocks of your application window.
Think of them as the controls you see in any desktop app:
- Labels (text)
- Buttons
- Text boxes
- Checkboxes
- Radio buttons
- Menus
- Frames (containers for grouping stuff)
Basically: everything you place inside your Tkinter window is a widget.
OK, now let's build a very simple profile window for editing a user profile.
Add a profile window
profile.py
import tkinter as tk
from tkinter import ttk
# Create main window
root = tk.Tk()
root.title("Profile Window")
root.geometry("400x500")
root.configure(bg="#f5f5f5")
# ---- Header ----
header = tk.Label(root, text="User Profile", font=("Arial", 18, "bold"), bg="#f5f5f5")
header.pack(pady=15)
# ---- Profile Picture (placeholder as a label) ----
profile_pic = tk.Label(root, text="👤", font=("Arial", 50), bg="#f5f5f5")
profile_pic.pack(pady=10)
# ---- Name ----
name_label = tk.Label(root, text="Name:", font=("Arial", 12), anchor="w", bg="#f5f5f5")
name_label.pack(fill="x", padx=20)
name_entry = tk.Entry(root, font=("Arial", 12))
name_entry.pack(fill="x", padx=20, pady=5)
# ---- Email ----
email_label = tk.Label(root, text="Email:", font=("Arial", 12), anchor="w", bg="#f5f5f5")
email_label.pack(fill="x", padx=20)
email_entry = tk.Entry(root, font=("Arial", 12))
email_entry.pack(fill="x", padx=20, pady=5)
# ---- Bio ----
bio_label = tk.Label(root, text="Bio:", font=("Arial", 12), anchor="w", bg="#f5f5f5")
bio_label.pack(fill="x", padx=20)
bio_text = tk.Text(root, height=5, font=("Arial", 12))
bio_text.pack(fill="x", padx=20, pady=5)
# ---- Save Button ----
def save_profile():
name = name_entry.get()
email = email_entry.get()
bio = bio_text.get("1.0", tk.END).strip()
print("Profile Saved:")
print(f"Name: {name}")
print(f"Email: {email}")
print(f"Bio: {bio}")
tk.messagebox.showinfo("Saved", "Profile information saved!")
save_button = ttk.Button(root, text="Save Profile", command=save_profile)
save_button.pack(pady=20)
root.mainloop()
Here we are
What we have done? We have made a profile window with some basic widgets:
- A header ("User Profile").
- A placeholder “profile picture” (just a Unicode 👤 for now).
- Fields for Name, Email, and Bio.
- A Save button that prints the data and shows a message box.
Conclusion
For a Python developer who already has a Python-based system and just needs a simple GUI layer on top of it, Tkinter is the ideal choice.
It’s built into Python, so there’s no extra dependency headache.
It integrates seamlessly with existing Python code — you can wrap scripts, functions, or system tools inside a small window with buttons and inputs.
Development is fast and straightforward; a few lines of Tkinter code can turn a command-line script into a usable desktop tool.
Top comments (0)