GUI in python
How to Build a GUI with Tkinter in Python
Graphical User Interfaces (GUIs) make applications user-friendly and visually appealing. Python’s Tkinter library is one of the simplest ways to create GUIs, making it a great choice for beginners and experienced developers alike. In this blog, we’ll walk through the basics of using Tkinter to create a functional GUI application.
What is Tkinter?
Tkinter is Python’s standard GUI library. It provides a fast and straightforward way to design and develop GUI applications. It comes pre-installed with Python, so there’s no need for additional installations.
Step 1: Setting Up Tkinter
To get started, you need to import Tkinter and create a main window.
import tkinter as tk# Create the main application window
root = tk.Tk()# Set the title of the window
root.title("My First GUI")# Set the window size
root.geometry("400x300")# Run the application
root.mainloop()
This script creates a basic window with a title and specified dimensions.
Step 2: Adding Widgets
Widgets are the building blocks of a Tkinter application. Common widgets include buttons, labels, entry boxes, and more.
Get Avish Joshi’s stories in your inbox
Join Medium for free to get updates from this writer.
Here’s how you can add some basic widgets:
# Adding a label
label = tk.Label(root, text="Welcome to My GUI!", font=("Arial", 16))
label.pack(pady=10)# Adding an entry field
entry = tk.Entry(root, width=30)
entry.pack(pady=5)# Adding a button
def greet():
user_name = entry.get()
greeting_label.config(text=f"Hello, {user_name}!")button = tk.Button(root, text="Submit", command=greet)
button.pack(pady=5)# Adding a dynamic label
greeting_label = tk.Label(root, text="", font=("Arial", 14))
greeting_label.pack(pady=10)
Step 3: Organizing the Layout
Tkinter offers three layout managers to position widgets: pack, grid, and place.
- Pack: Stacks widgets vertically or horizontally.
- Grid: Arranges widgets in a grid format.
- Place: Places widgets at specific coordinates.
Here’s an example using the grid layout:
# Using grid layout
label.grid(row=0, column=0, columnspan=2, pady=10)
entry.grid(row=1, column=0, columnspan=2, pady=5)
button.grid(row=2, column=0, pady=5)
greeting_label.grid(row=3, column=0, columnspan=2, pady=10)Step 4: Adding Style
Tkinter supports styling through its ttk module. You can use themed widgets and styles for a modern look.
from tkinter import ttk# Create a styled button
styled_button = ttk.Button(root, text="Styled Button")
styled_button.pack(pady=10)Step 5: Finalizing and Running the App
Combine all the elements into a complete application:
import tkinter as tk
from tkinter import ttkdef greet():
user_name = entry.get()
greeting_label.config(text=f"Hello, {user_name}!")# Main window
root = tk.Tk()
root.title("My First GUI")
root.geometry("400x300")# Widgets
label = tk.Label(root, text="Welcome to My GUI!", font=("Arial", 16))
label.pack(pady=10)entry = tk.Entry(root, width=30)
entry.pack(pady=5)button = tk.Button(root, text="Submit", command=greet)
button.pack(pady=5)greeting_label = tk.Label(root, text="", font=("Arial", 14))
greeting_label.pack(pady=10)# Run the application
root.mainloop()
Conclusion
Building a GUI with Tkinter is straightforward and fun. With practice, you can create complex applications by combining widgets, layouts, and logic. Start experimenting with Tkinter and build something amazing today!






