Here’s an example of how to build a basic GUI application in Python using the Tkinter library:
import tkinter as tk
self.label = tk.Label(self, text="Welcome to my simple app!")
self.button = tk.Button(self, text="Click me!", command=self.on_button_click)
if __name__ == "__main__":
app = SimpleApp()
This code creates a simple GUI application that has a label and a button. The label displays a welcome message, and the button displays the text “Click me!” When the button is clicked, the on_button_click function is called, which changes the text displayed on the label to “Button clicked!”.
Tk class is the main class of the Tkinter library and it creates the main window of the application. The
title method sets the title of the main window and the
geometry method sets the size of the main window.
Button classes are both used to create widgets that are placed inside the main window. The
pack method is used to add the widgets to the main window.
on_button_click function is the event handler for the button click event. It is called when the button is clicked.
mainloop method is used to start the event loop of the application, which waits for user input and handles events, such as button clicks.
You can add more widgets, such as text box, checkbox, radio button and etc. to the application by creating instances of the appropriate classes and using the
pack method to add them to the main window.
It’s a good idea to start with a simple application and then gradually add more functionality as you become more familiar with the library.
Run the complete code and this will be the result.
Learn More on TKinter GUI
- How to Create Conutdown Timer with Python & TKinter
- Create GUI Applications with Python & TKinter
- Python TKinter Layout Management
- How to Create Label in TKinter
- How to Create Buttin in Python TKinter
- Build Music Player in Python TKinter
- How to Build Calculator in Python TKinter