In this Python PyQt6 article we are going to learn about How to Create Custom Dialogs in Python PyQt6, so PyQt6 is powerful and popular Python library for creating graphical user interfaces (GUIs). and in this article we want to learn about How to Create Custom Dialogs in Python PyQt6.
You can install PyQt6 using pip command
1 |
pip install PyQt6 |
This is the complete code for this article
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import sys from PyQt6.QtWidgets import QApplication, QDialog, QVBoxLayout, QLabel, QLineEdit, QPushButton class CustomDialog(QDialog): def __init__(self): super().__init__() # Set title and dimensions of the dialog self.setWindowTitle("Custom Dialog") self.setFixedSize(300, 150) # Create QVBoxLayout instance to hold the # dialog's widgets layout = QVBoxLayout() # Create QLabel instance for the dialog's instructions label = QLabel("Please enter your name:") layout.addWidget(label) # Create QLineEdit instance for the user's input self.input = QLineEdit() layout.addWidget(self.input) # Create QPushButton instance for submitting the user's input button = QPushButton("Submit") button.clicked.connect(self.submit) layout.addWidget(button) # Set the dialog's layout self.setLayout(layout) def submit(self): # Get the user input from the QLineEdit instance name = self.input.text() # Print the user input to the console print("Hello, " + name + "!") # Close dialog self.close() if __name__ == '__main__': app = QApplication(sys.argv) # Create instance of the custom dialog dialog = CustomDialog() # Show dialog dialog.exec() # Exit application sys.exit(app.exec()) |
In this above code first of the necessary modules are imported including QApplication, QDialog, QVBoxLayout, QLabel, QLineEdit and QPushButton.
1 2 |
import sys from PyQt6.QtWidgets import QApplication, QDialog, QVBoxLayout, QLabel, QLineEdit, QPushButton |
After that new class called CustomDialog is defined, which inherits from QDialog. in the constructor for this class, the dialog’s window title and dimensions are set, and new QVBoxLayout instance is created to hold the dialog widgets.
1 2 3 |
self.setWindowTitle("Custom Dialog") self.setFixedSize(300, 150) layout = QVBoxLayout() |
In here new QLabel instance is created to display instructions to the user and added to the layout. QLineEdit instance is also created to allow the user to input text and added to the layout. and finally QPushButton instance is created to submit user input, and a clicked signal is connected to a new submit method, which retrieves the user’s input and prints it to the console.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
label = QLabel("Please enter your name:") layout.addWidget(label) self.input = QLineEdit() layout.addWidget(self.input) button = QPushButton("Submit") button.clicked.connect(self.submit) layout.addWidget(button) self.setLayout(layout) def submit(self): name = self.input.text() print("Hello, " + name + "!") self.close() |
And finally in the __main__ block new QApplication instance is created and an instance of the custom dialog is created and shown using the exec() method. the application event loop is then started with app.exec().
1 2 3 4 5 |
if __name__ == '__main__': app = QApplication(sys.argv) dialog = CustomDialog() dialog.exec() sys.exit(app.exec()) |
Run your complete code and this is the result
Learn More on Python GUI
- How to Use Stylesheets in PyQt5
- How to Build Custom Widgets in PyQt6
- How to Create CheckButton in Python TKinter
- Object Tracking with Python & OpenCV
- How to Create Layouts in Python PyQt6
- How to Create RadioButton in PySide6
- How to Create ComboBox in PySide6
- How to Create CheckBox in Python PySide6
- Responsive Applications with PyQt6 Multithreading
- Event Handling in Python and PyQt6
- How to Use Stylesheets in Python PyQt6