In this article we want to learn How to Build Basic GUI Application in Python, so when it comes to building GUI applications with Python you have different options and libraries, we are going to talk about some of them and we will create some practical example about these GUI Frameworks in Python.
Why Python for GUI?
Python is one of the popular programming languages, Python has easy syntax and large community support. When it comes to GUI development, Python shines with its differtent libraries such as Tkinter, PyQt, and Kivy, each of them offers unique features and advantages. These libraries abstract away much of the complexity of GUI development, and allows you to focus on creating nice and beautiful interfaces.
Build Basic GUI Application in Python TKinter?
So first let’s start building our first basic GUI application with Python TKinter, Tkinter is Python library that helps you create graphical user interfaces (GUIs) for your programs. It provides different components, like buttons, labels and entry fields, that you can use to build windows, menus and other visual elements for your applications.
How to install Python TKinter?
TKinter is already packed with Python, so you don’t need to install that.
So now this is a simple code for building GUI application with Python TKinter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# Import the tkinter module and alias it as 'tk' import tkinter as tk class SimpleApp(tk.Tk): def __init__(self): # Call the constructor of the superclass (tk.Tk) super().__init__() # Set the title of the window self.title("Codeloop - Simple App") # Set the size of the window to 300x200 pixels self.geometry("300x200") # Create a label widget with text self.label = tk.Label(self, text="Codeloop.org") # Add the label widget to the window and display it self.label.pack() # Create a button widget with text and a command self.button = tk.Button(self, text="Click me", command=self.on_button_click) # Add the button widget to the window and display it self.button.pack() def on_button_click(self): # Change the text of the label when the button is clicked self.label.config(text="Welcome to codeloop.org") # Change the text of the label when the button is clicked if __name__ == "__main__": # Create an instance of the SimpleApp class app = SimpleApp() # Start the Tkinter event loop to display the # window and handle user interactions app.mainloop() |
This code creates a simple graphical user interface (GUI) application using Tkinter. When you run the code, it opens a window with a label displaying “Codeloop.org” and a button labeled “Click me”. When you click the button, the label text changes to “Welcome to codeloop.org”. The application continues to run until you close the window. Essentially, it’s a basic example demonstrating how to create a GUI with a label and a button that performs an action when clicked.
Subscribe and Get Free Video Courses & Articles in your Email