Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to make a gui in Python

Getting Started with GUI in Python

As you step into the world of programming, you'll come across the need to create an interface for your programs. This is where Graphical User Interface (GUI) comes in. GUI is like a friendly face of your program that users interact with. Think of it as the pretty, user-friendly layer over your lines of code.

In Python, we have several libraries (collection of reusable pieces of code) to create GUIs. In this guide, we're going to use Tkinter, a popular and easy-to-use library. Don't worry, we're not going to leave you alone in the jungle of code. We'll walk you through it step by step, like a friend holding your hand on a hike.

Setting Up Tkinter

Before we start, we need to make sure that Tkinter is ready to use. The good news is, if you're using a standard Python installation, Tkinter comes pre-installed. So, you're all set!

Creating Your First Window

Creating a window is like creating a canvas. It's the starting point of your GUI. Here's how you do it:

import tkinter as tk

root = tk.Tk()
root.mainloop()

When you run this code, you'll see an empty window. Congrats! You just created your first GUI in Python. In this code, tkinter is our toolbox, and Tk() is a tool from that box that we used to create the window.

Adding Some Widgets

Widgets are the building blocks of a GUI. They're like Lego pieces. Think of buttons, labels, and text boxes - these are all widgets.

Let's add a label widget to our window:

import tkinter as tk

root = tk.Tk()

label = tk.Label(root, text="Hello, GUI!")
label.pack()

root.mainloop()

When you run this code, you'll see a window with the text "Hello, GUI!". We just added a label widget to our window. Label() is a tool from our tkinter toolbox, and pack() is like the glue that sticks the widget onto the window.

Handling User Actions

A GUI isn't much fun if it doesn't respond to user actions. Let's add a button that, when clicked, changes the text of our label. This is like adding a light switch that turns on the light when flipped.

import tkinter as tk

root = tk.Tk()

label = tk.Label(root, text="Hello, GUI!")
label.pack()

def change_text():
    label.config(text="You clicked the button!")

button = tk.Button(root, text="Click me", command=change_text)
button.pack()

root.mainloop()

In this script, we created a button widget and a function change_text(). The Button() tool from our tkinter toolbox takes an extra parameter command that tells it what function to execute when clicked. In our case, it's the change_text() function, which changes the label text.

Organizing Widgets with Frames

When your GUI starts to grow, you'll want to organize your widgets. Frames are like containers or boxes that you can put your widgets in. Let's put our label and button into a frame:

import tkinter as tk

root = tk.Tk()

frame = tk.Frame(root)
frame.pack()

label = tk.Label(frame, text="Hello, GUI!")
label.pack()

def change_text():
    label.config(text="You clicked the button!")

button = tk.Button(frame, text="Click me", command=change_text)
button.pack()

root.mainloop()

In this code, Frame() creates a new frame. We then put our label and button into this frame by changing their parent from root to frame.

Conclusion: The Art of GUI in Python

Coding GUI in Python is like painting on a canvas. The tkinter library is your palette, widgets are your colors, and frames are your brushes. As you continue to explore and experiment, you'll create interactive and user-friendly interfaces that bring your code to life.

Remember, every great artist was once an amateur. Don't be afraid to make mistakes and learn from them. Happy coding, and here's to creating your masterpiece in Python!