Python PyQt5 basic application with widgets
Python PyQt Basic: Exercise-2 with Solution
Write a Python program to create a basic PyQt application that opens an empty window with a specified size and title. Add various widgets (e.g. label, push button) to the blank window. Widgets should display text or perform a simple action.
QApplication Class: The QApplication class manages the GUI application's control flow and main settings.
QMainWindow Class: The QMainWindow class provides a main application window.
QLabel Class: The QLabel widget provides a text or image display.
QPushButton: The push button, or command button, is perhaps the most commonly used widget in any graphical user interface. Push (click) a button to command the computer to perform some action, or to answer a question. Typical buttons are OK, Apply, Cancel, Close, Yes, No and Help.
QVBoxLayout Class: The QVBoxLayout class lines up widgets vertically.
QWidget: The QWidget class is the base class of all user interface objects.
import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QVBoxLayout, QWidget def main(): # Create a PyQt application app = QApplication(sys.argv) # Create a QMainWindow (main window) main_window = QMainWindow() # Set the window properties (title and initial size) main_window.setWindowTitle("Basic PyQt Application") main_window.setGeometry(100, 100, 400, 300) # (x, y, width, height) # Create a central widget for the main window central_widget = QWidget() main_window.setCentralWidget(central_widget) # Create widgets (QLabel and QPushButton) label = QLabel("Example of PyQt label!") button = QPushButton("Example of PyQt pushbutton!") # Create a layout to arrange the widgets vertically layout = QVBoxLayout() layout.addWidget(label) layout.addWidget(button) # Set the layout for the central widget central_widget.setLayout(layout) # Show the window main_window.show() # Run the application's event loop sys.exit(app.exec_()) if __name__ == "__main__": main()
In the exercise above -
- Import the necessary modules from PyQt5.
- Create a "QApplication" object to manage application control flow and settings.
- Create a "QMainWindow" object for the main application window and set its title and initial size.
- Create a central widget (a QWidget) to hold our other widgets.
- Create two widgets: a QLabel displaying "Example of PyQt label!" and a QPushButton with the label "Example of PyQt pushbutton!".
- Create a vertical layout (QVBoxLayout) and add 'QLabel' and 'QPushButton' to it.
- Set the layout for the central widget so that it arranges the widgets vertically.
- Finally, start the application's event loop with app.exec_() to handle user interactions and events.
Python Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Summing a sequence of numbers (calculating the sum of zero to ten with skips):
>>> l = range(0,10,2) >>> sum(l) 20
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook