When beginners start learning Python, they usually write programs that run in a terminal. You enter a command, press Enter, and receive text as output. But what if you want to create a program with a window, buttons, text fields, and menus?
This is where a GUI (Graphical User Interface) becomes useful.
A common problem for new Python developers is assuming that creating a desktop application requires a complicated framework or a large amount of code. In reality, Python includes a built-in GUI library called Tkinter. It lets you create simple desktop applications without installing a separate GUI framework.
In this tutorial, you will build your first Tkinter application step by step. By the end, you will have a small greeting app with a text input, a button, and a message displayed inside the window.
1. What Is Tkinter?

Tkinter is Python’s standard interface to the Tcl/Tk GUI toolkit. It provides the basic components needed to build desktop applications, including:
- Windows and dialog boxes
- Buttons and labels
- Text input fields
- Menus and checkboxes
- Layout controls
Tkinter is included with many Python installations, so you may not need to install anything extra.
It is a good starting point for beginners because you can learn the basic principles of event-driven programming without first learning a large framework.
2. Check Your Python Installation

Before creating a GUI application, make sure Python is installed correctly.
Step 1: Open the Terminal
On Windows, open:
Start > Search > Command Prompt
You can also use the integrated terminal in Visual Studio Code.
Run this command:
python --version
If Windows does not recognize the command, try:
py --version
You should see a Python version number, such as:
Python 3.12.0
The exact version may be different.
Step 2: Check Whether Tkinter Is Available
Run:
python -m tkinter
If Tkinter is installed correctly, a small test window should appear.
Close that window after checking it.
If the command reports that the tkinter module is missing, your Python installation may not include the Tcl/Tk components. On Windows, rerun the official Python installer and check that the Tcl/Tk and IDLE option is selected.
3. Create Your First Tkinter Window

Now you can create your first GUI program.
Step 1: Create a Python File
Open Visual Studio Code or another code editor.
Create a folder for your project, such as:
PythonProjects
└── tkinter_first_app
└── main.py
Open main.py and add the following code:
import tkinter as tk
# Create the main application window
root = tk.Tk()
# Set the window title
root.title("My First Tkinter App")
# Set the window size
root.geometry("400x300")
# Start the application event loop
root.mainloop()
Save the file.
Step 2: Run the Program
Open the terminal in your project folder and execute:
python main.py
A blank application window should appear.
You have now created your first Python GUI application.
The code uses four important elements:
tk.Tk()creates the main window.title()sets the window title.geometry()controls its initial size.mainloop()keeps the window open and processes user interactions.
The event loop is essential. Without it, the application may close immediately after creating the window.
4. Add Labels, Text Fields, and Buttons

A blank window is not very useful. Let’s add some controls.
Tkinter provides several widgets, and three of the most common are:
Label: Displays text.Entry: Accepts a single line of user input.Button: Runs a function when clicked.
Replace your existing code with this example:
import tkinter as tk
root = tk.Tk()
root.title("My First Tkinter App")
root.geometry("400x300")
# Add a heading
heading = tk.Label(
root,
text="Welcome to My App",
font=("Arial", 18)
)
heading.pack(pady=20)
# Add a text input
name_input = tk.Entry(root, width=30)
name_input.pack(pady=10)
# Add a label for the result
result_label = tk.Label(root, text="Enter your name below")
result_label.pack(pady=10)
root.mainloop()
Save and run the program again.
You should now see a heading, an input field, and a result label.
Understand the pack() Layout Manager
You may have noticed the pack() method.
Tkinter uses layout managers to position widgets inside a window. The three main options are:
pack(): Arranges widgets in blocks.grid(): Places widgets in rows and columns.place(): Uses specific coordinates.
For a small beginner application, pack() is easy to understand. Larger interfaces often benefit from grid() because it provides more control over alignment.
5. Make the Button Respond to User Input

A GUI application becomes interactive when widgets respond to events.
Let’s add a button that reads the name entered by the user and displays a greeting.
Replace the previous code with the complete version below:
import tkinter as tk
def show_greeting():
name = name_input.get().strip()
if name:
result_label.config(
text=f"Hello, {name}! Welcome to Python GUI."
)
else:
result_label.config(
text="Please enter your name first."
)
root = tk.Tk()
root.title("My First Tkinter App")
root.geometry("400x300")
heading = tk.Label(
root,
text="Welcome to My App",
font=("Arial", 18)
)
heading.pack(pady=20)
name_input = tk.Entry(root, width=30)
name_input.pack(pady=10)
greeting_button = tk.Button(
root,
text="Say Hello",
command=show_greeting
)
greeting_button.pack(pady=10)
result_label = tk.Label(
root,
text="Enter your name below"
)
result_label.pack(pady=10)
root.mainloop()
Run the program:
python main.py
Enter your name and click Say Hello.
The application should display a personalized greeting.
How the Button Event Works
The key part is:
command=show_greeting
This tells Tkinter which function to execute when the button is clicked.
Inside the function, name_input.get() retrieves the text from the input field. The strip() method removes unnecessary spaces at the beginning and end.
Finally, result_label.config() updates the text displayed in the window.
Notice that we use command=show_greeting, not command=show_greeting(). Adding parentheses would call the function immediately instead of waiting for the button click.
6. Improve the Application’s Appearance

You can make a basic Tkinter application look more organized by adjusting colors, fonts, spacing, and window properties.
For example, add a background color and improve the button styling:
root.configure(bg="#f2f4f7")
heading.config(
bg="#f2f4f7",
fg="#1f2937"
)
result_label.config(
bg="#f2f4f7",
fg="#2563eb",
font=("Arial", 11)
)
greeting_button.config(
bg="#2563eb",
fg="white",
padx=15,
pady=5
)
Place these lines after the widgets have been created but before root.mainloop().
You can experiment with different colors and fonts to create an interface that suits your project.
7. Common Mistakes and Troubleshooting
Python Cannot Find the Tkinter Module
If you see an error such as ModuleNotFoundError: No module named 'tkinter', your Python installation may be missing the Tcl/Tk components.
Solution: On Windows, modify or reinstall Python using the official installer and ensure that Tcl/Tk and IDLE are selected. On Linux, you may need to install the distribution’s Tkinter package separately.
The Application Window Opens and Closes Immediately
This usually happens when the event loop is missing.
Solution: Make sure the final line of your program is:
root.mainloop()
The Button Does Nothing
Check that the function exists and that the button uses the correct callback:
command=show_greeting
Also check that you have not accidentally added parentheses.
The Widgets Are Not Positioned Correctly
Mixing layout managers inside the same parent container can cause layout problems.
Solution: Use one layout manager consistently for widgets that share the same parent. You can use different layout managers inside separate frames when necessary.
The Program Runs in the Wrong Python Environment
If Tkinter works in Command Prompt but fails in Visual Studio Code, the editor may be using a different Python interpreter.
Solution: In VS Code, open the Command Palette with Ctrl + Shift + P, choose Python: Select Interpreter, and select the installation you tested.
Final Thoughts
Tkinter is one of the easiest ways to create your first desktop application with Python. With only a few lines of code, you can create a window, add input fields, display information, and respond to button clicks.
The greeting application in this tutorial is a small starting point. Once you understand windows, widgets, layout managers, and event handling, you can build more useful tools such as calculators, note-taking applications, file organizers, and simple data-entry programs.
Start with a small project, test each feature, and gradually add more functionality as your Python skills improve.
