Based on contributions by LuisdeSantiago.
Tkinter is Python’s built-in GUI toolkit. Use it to build a simple desktop control panel (buttons, labels, dropdowns) without installing anything extra. This guide covers the basics: creating a window, adding widgets, and the two most common layout methods.
What you need
- Python 3 with Tkinter (included in the standard library on Windows and macOS; on Linux you may need to install it separately, e.g.
sudo apt install python3-tk). - No other packages required.
For a more modern look and additional styling options, see How to style a Tkinter interface, and how to switch to CustomTkinter (which also covers CustomTkinter).
Steps
1. Import Tkinter
import tkinter as tk
2. Create the main window
window = tk.Tk()
window.title("My First Interface with Tkinter")
window.geometry("400x300") # width x height in pixels
3. Run the main application loop
mainloop() keeps the window open and listening for user actions. It must be the last thing your script calls, after all widgets are created.
window.mainloop()
4. Add a label
A Label displays text.
label = tk.Label(window, text="Hello, welcome to my application!")
label.pack() # place the widget in the window
5. Add a text entry field
An Entry widget lets the user type text.
entry = tk.Entry(window)
entry.pack()
6. Add a button with an action
Define a function to run when the button is clicked, then attach it with command=.
def show_text():
text = entry.get() # read the text from the entry field
label.config(text="Hello, " + text) # update the label
button = tk.Button(window, text="Show text", command=show_text)
button.pack()
7. Add a dropdown list
options = ["Option 1", "Option 2", "Option 3", "Option 4"]
selected_option = tk.StringVar()
selected_option.set(options[0]) # set the initial option
menu_options = tk.OptionMenu(window, selected_option, *options)
menu_options.pack()
def show_selection():
selection = selected_option.get()
label.config(text="You selected: " + selection)
show_button = tk.Button(window, text="Show selection", command=show_selection)
show_button.pack()
8. Choose a layout method
Tkinter has three layout managers; the two most common are pack() and place(). Don’t mix them on widgets that share the same parent — pick one per container.
pack() stacks widgets vertically or horizontally, depending on the side option:
side: which side of the container the widget attaches to (top,bottom,left,right).fill: lets the widget stretch to fill available space (x,y, orboth).padx,pady: padding around the widget, in pixels.
import tkinter as tk
window = tk.Tk()
window.title("Alignment with pack()")
window.geometry("300x200")
label1 = tk.Label(window, text="Top", bg="lightblue")
label1.pack(side="top", fill="x", padx=10, pady=5)
label2 = tk.Label(window, text="Bottom", bg="lightgreen")
label2.pack(side="bottom", fill="x", padx=10, pady=5)
label3 = tk.Label(window, text="Left", bg="lightcoral")
label3.pack(side="left", fill="y", padx=5, pady=10)
label4 = tk.Label(window, text="Right", bg="lightpink")
label4.pack(side="right", fill="y", padx=5, pady=10)
window.mainloop()
label1 sits at the top and stretches across the full width; label2 does the same at the bottom; label3 and label4 sit on the left and right and stretch the full height.
place() positions widgets using exact pixel or relative coordinates:
x,y: pixel coordinates of the widget.relx,rely: relative coordinates from 0 to 1 (0.5 = center).anchor: which point of the widget is placed at that coordinate (e.g."center","se"for bottom-right).
import tkinter as tk
window = tk.Tk()
window.title("Alignment with place()")
window.geometry("300x200")
label1 = tk.Label(window, text="Top-left corner", bg="lightblue")
label1.place(x=10, y=10)
label2 = tk.Label(window, text="Center", bg="lightgreen")
label2.place(relx=0.5, rely=0.5, anchor="center")
label3 = tk.Label(window, text="Bottom-right corner", bg="lightcoral")
label3.place(relx=1.0, rely=1.0, anchor="se", x=-10, y=-10)
window.mainloop()
label1 sits 10 pixels from the top-left corner; label2 sits exactly in the center using relative coordinates; label3 sits in the bottom-right corner, offset 10 pixels inward with x=-10, y=-10.
pack()is easiest for small, simple interfaces. For larger interfaces where you need precise control over rows and columns (and where the layout should scale with the window), usegrid()— see How to style a Tkinter interface, and how to switch to CustomTkinter for an example that combinesgrid()with styled buttons.
Common mistakes
- Calling
mainloop()before creating all your widgets. Anything added aftermainloop()runs will not appear, since the script blocks there until the window closes. - Mixing
pack()andgrid()on widgets with the same parent. Tkinter will raise an error or produce a broken layout — pick one layout manager per container. - Forgetting to reassign the returned value of
StringVar()/similar — always call.get()to read the current value, not the variable itself.
Related
Rewritten and consolidated (Sept 2026) from the original student how-to’s: How to create an interface in python using tkinter.