Python’s Modern Graphical User Interfaces

Posted by


Modern Graphical User Interfaces (GUIs) are an essential part of any software application that aims to provide a user-friendly experience. In this tutorial, we will learn how to create modern GUIs in Python using Tkinter, a built-in GUI library for Python.

Step 1: Installing Tkinter
Tkinter is already included with Python installation, so you don’t need to install it separately. However, if you are using a virtual environment, make sure to activate it before importing Tkinter.

Step 2: Importing Tkinter
To start creating a GUI application, we first need to import the Tkinter module. You can do this by adding the following line at the beginning of your Python script:

import tkinter as tk

Step 3: Creating a GUI window
Now, let’s create a basic window for our GUI application. We can do this by creating an instance of the Tk class, which represents the main window of the application:

root = tk.Tk()
root.title("Modern GUI Tutorial")

This code snippet creates a window with the title "Modern GUI Tutorial".

Step 4: Adding widgets to the GUI
Widgets are the building blocks of a GUI application. We can add various widgets such as buttons, labels, entry fields, and more to our GUI window. Here’s an example of adding a button widget to our GUI window:

button = tk.Button(root, text="Click Me")
button.pack()

This code snippet creates a button with the text "Click Me" and adds it to the window using the pack() method.

Step 5: Styling the GUI
To create a modern-looking GUI, we can customize the appearance of our widgets using various styling options provided by Tkinter. We can change the font, color, size, and more of the widgets. Here’s an example of styling the button widget:

button.config(font=("Arial", 12), fg="white", bg="blue")

This code snippet changes the font to Arial with size 12, sets the text color to white, and the background color to blue.

Step 6: Handling events
GUI applications often involve handling user interactions such as button clicks, mouse events, and keyboard inputs. We can bind events to widgets by specifying a callback function that gets executed when the event occurs. Here’s an example of handling a button click event:

def on_click():
    print("Button clicked!")

button.config(command=on_click)

This code snippet creates a callback function called on_click() that prints "Button clicked!" when the button is clicked. We then assign this function to the button widget using the command parameter.

Step 7: Main event loop
Finally, to run the GUI application, we need to start the main event loop. This loop listens for events such as user interactions and updates the GUI accordingly. We can start the main event loop by calling the mainloop() method on the root window:

root.mainloop()

This code snippet starts the main event loop of the GUI application, allowing users to interact with the widgets.

Congratulations! You have now successfully created a modern GUI application in Python using Tkinter. Feel free to explore more widgets, styling options, and event handling techniques to create more sophisticated GUI applications. Happy coding!

0 0 votes
Article Rating
31 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@CherrineShortdy
2 months ago

Moore Amy Wilson Kimberly Walker Lisa

@NancyDell-m6y
2 months ago

Hall Laura Young Cynthia Taylor Larry

@PHOENIXMARIE-t7y
2 months ago

"Could I have your WhatsApp number so we can collaborate on some projects more effectively? I would greatly appreciate your help in expanding my code. Thank you." "Whenever I've reached out to programmers with a request, they've unfortunately been unable to assist due to time constraints, and my requests for collaboration have been declined. I kindly ask that you consider responding positively to my request just this once. My request is for you to review the code I've written ."

@maty7778
2 months ago

Thank you for your videos NeuralNine. They've been very inspirational for working with projects, and I have been recommending you a lot for people learning about Python due to the vast amount of content you create for it. So thank you again for what you do.

@amolmanorkar2841
2 months ago

This is really helpful!. Thanks a lot for all explanation:)

@justrobin2176
2 months ago

Im sorry I have mac so you lost me in the first second

@mawkuri5496
2 months ago

can you make a formation studio tutorial

@curtislevey7639
2 months ago

I'm trying to implement this GUI into the secure login from the other video as suggested.

Having trouble getting the gui to link up with the back end – server communication

@TipsJazzInferno
2 months ago

litarly unfair on youtube beacuse i upload they same video in my channel 2 years ago but still no comment no views and you upload video new and you got in millionw views even you use custom tkinter but i used only tkinter pure tkinter to make modern gui 🙁

@chrismiller9611
2 months ago

Help, please! I am new to Python. When running the program, I get the error that module customtkinter is not found yet the install shows that it is there and is satisfied. Running Python 3.12 in Visual Studio Code. ModuleNotFoundError: No module named 'customtkinter'
This occurs at the import line.

@jordanhenshaw
2 months ago

Progress bar connected to a slider sounds pretty dope.

@C4nadianB4con
2 months ago

i have an issue with root.geometry(). i get an error that says "attribute error: 'str' object has no attribute '_reverse_geometry_scaling'

@Acanis87
2 months ago

Iam new to Python and not so experienced as developer. Should I learn to create UIs like that or better use a UI Designer? Always used UI Designer before, seems to be hard to it without seeing anything while creating a UI?!

@MaskVoff
2 months ago

do you have any condition of copyright to use it ? 🙂

@MotoSwagger
2 months ago

Awesome Turorial @NeuralNine – I couldn't find complex_example.py in your GitHub repo???

@nagisaphantom
2 months ago

import customtkinter

customtkinter.set_appearance_mode("dark")

customtkinter.set_default_color_theme("dark-blue")

root = customtkinter.CTk()

root.geometry("500×350")

def login():

print("Test")

frame = customtkinter.CTkFrame(master=root)

frame.pack(pady=20, padx=60, fill="both", expand=True)

label = customtkinter.CTkLabel(master=frame, text="Login System", font=("Roboto", 24))

label.pack(pady=12, padx=10)

entry1 = customtkinter.CTkEntry(master=frame, placeholder_text="Username")

entry1.pack(pady=12, padx=10)

entry2 = customtkinter.CTkEntry(master=frame, placeholder_text="Passwort", show="*")

entry2.pack(pady=12, padx=10)

button = customtkinter.CTkButton(master=frame, text="Login", command=login)

button.pack(pady=12, padx=10)

checkbox = customtkinter.CTkCheckBox(master=frame, text="Remember Me")

checkbox.pack(pady=12, padx=10)

root.mainloop()

@Adriangaming2000
2 months ago

THANK YOU SO MUCH!!!!!

@danboudreault4422
2 months ago

Does Python has a graphical IDE that you can just draw the UI like Visual Studios?

@hakanloyan3483
2 months ago

Bro if u used Nvidia thing for looking at middle of the camera it's scary do not do that plz

@muslimgamerrr9479
2 months ago

YES its so simple thanks so much. now i can make a gui for my wallpaper setter