Creating a custom widget in PyQt
Write a Python program using PyQt5 that asks users to change widget properties, such as color, font, or size. Observe how it affects the widget's appearance.
From doc.qt.io:
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.
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: This class is used to construct vertical box layout objects.
QWidget: The QWidget class is the base class of all user interface objects.
QColorDialog Class: The QColorDialog class provides a dialog widget for specifying colors.
QFontDialog Class: The QFontDialog class provides a dialog widget for selecting a font.
Sample Solution:
Python Code:
Explanation:
In the exercise above -
- Import the necessary modules.
- Create a custom "WidgetPropertiesApp" class that inherits from 'QMainWindow'.
- Inside the class, we create the main window and a 'QPushButton' that can be customized by the user.
- Create customization methods "customize_color()" and "customize_font()" that allow the user to select custom colors and fonts for the button.
- Connect the button's click events to these customization methods using 'clicked.connect'.
- The main function creates an instance of WidgetPropertiesApp, displays the window, and starts the application's event loop.
Output:
Flowchart:


Go to:
Previous: Creating a custom widget with PyQt.
Next: Building a basic calculator with Python and PyQt.
Python Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.