For creating GUI in python we have multiple choices like tkinter ,WXPython, Kivy and PyQt5 , a cross platform very famous library for creating Graphical User Interfaces as well as cross-platform applications, in this tutorial we will use it for creating GUIs.
Installation:
For Windows :
pip install PyQt5
For Linux & MacOS:
pip3 install PyQt5
Hello World! The traditional beginning:
In this tutorial we will make a simple app showing "Hello World!" after pressing a button, so let's start.
# importing the required libraries
from PyQt5.QtWidgets import *
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
# set the title
self.setWindowTitle("Hello World!")
# set the geometry
self.setGeometry(0, 0, 300, 300)
# create label widget
# to display content on screen
self.label = QLabel("Hello World !!", self)
# show all the widgets
self.show()
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
The Result:
First of all we have created the Window class which inherits QMainWindow class. Within this class we can add widgets which will get displayed on main window, in the next step we have used setWindowTiltle method to set title for our window, setGeometry method to set size and position of the window and , QLabel for displaying message in the window and the show method for showing all the widgets.
The QApplication class manages the GUI application’s control flow and main settings. It specializes in the QGuiApplication with some functionality needed for QWidget based applications. It handles widget specific initialization, finalization. For any GUI application using Qt, there is precisely one QApplication object, no matter whether the application has 0, 1, 2, or more windows at any given time.
Top comments (0)