In the previous lesson we have learned that how we can create our first window with PySide6 In this lesson we want to talk about Python PySide6 Window Type Classes.
PySide6 provides different types of window classes for building different types of windows in desktop applications. these are some of key window type classes that are available in PySide6:
- QMainWindow: This is main window class in PySide6 and provides a standard main application window with a menu bar, toolbar, and status bar.
- QDialog: This class provides a dialog box that can be used to display messages or prompt the user for input.
- QMessageBox: This class provides a pre-built dialog box for displaying informative or error messages to the user.
- QFileDialog: This class provides a dialog box for selecting files or directories from the file system.
- QInputDialog: This class provides a dialog box for prompting the user to enter a value.
- QProgressDialog: This class provides a dialog box for displaying the progress of a long-running operation.
- QDockWidget: This class provides a widget that can be docked to the edges of a QMainWindow.
- QSplashScreen: This class provides a splash screen that can be displayed while the application is loading.
- QTabWidget: This class provides a widget that can display multiple tabs, each with its own set of content.
we can say that PySide6 provides rich set of window type classes that can be used to build different types of desktop applications, from simple dialog boxes to complex main application windows. By leveraging these classes, developers can create applications that are both powerful and intuitive to use.
These are examples of using QDialog
and QMainWindow
in PySide6:
Example 1: Using QDialog
1 2 3 4 5 6 7 8 9 10 11 12 13 |
from PySide6.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton app = QApplication([]) window = QMainWindow() window.setWindowTitle("My App") label = QLabel("Hello, GeeksCoders!") button = QPushButton("Click me") window.setCentralWidget(label) window.addToolBar("My toolbar").addWidget(button) window.show() app.exec() |
In this example, we create simple QDialog with QLabel and QPushButton, and setLayout()
method is used to set QVBoxLayout as the layout for the dialog, and exec()
method is used to display the dialog and wait for the user to interact with it.
Run the code and this will be the output.

Example 2: Using QMainWindow
1 2 3 4 5 6 7 8 9 10 11 12 13 |
from PySide6.QtWidgets import QApplication, QDialog, QLabel, QVBoxLayout, QPushButton app = QApplication([]) dialog = QDialog() layout = QVBoxLayout() label = QLabel("Hello, GeeksCoders!") button = QPushButton("GeeksCoders") layout.addWidget(label) layout.addWidget(button) dialog.setLayout(layout) dialog.exec() |
In this example, we create QMainWindow with QLabel and QPushButton. also setWindowTitle()
method is used to set the title for the window, and setCentralWidget()
method is used to set the label as the central widget for the window. also addToolBar()
method is used to add a toolbar to the window andaddWidget()
method is used to add the button to the toolbar. alsoshow()
method is used to display the window and exec()
method is used to start the application event loop and wait for the user to interact with the window.
Run the code and this will be the result.

Difference between QDialog and QMainWindow
main difference between QDialog
and QMainWindow
is that QDialog
is typically used to display modal or non-modal dialog boxes andQMainWindow
is typically used as the main application window. QMainWindow
provides more complete set of features for building main application windows, such as menu bar, toolbar, and status bar, while QDialog
provides simpler set of features for building dialog boxes.
These are the examples of using QDialog
and QMainWindow
with object-oriented programming (OOP) in PySide6.
QDialog with OOP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
from PySide6.QtWidgets import QApplication, QDialog, QLabel, QVBoxLayout, QPushButton class MyDialog(QDialog): def __init__(self): super().__init__() layout = QVBoxLayout() label = QLabel("Hello, GeeksCoders!") button = QPushButton("OK") layout.addWidget(label) layout.addWidget(button) self.setLayout(layout) self.exec() app = QApplication([]) dialog = MyDialog() app.exec() |
In this example, we create MyDialog
class that inherits from QDialog
. and__init__
method is used to initialize the dialog by creating a QVBoxLayout, a QLabel and a QPushButton, setting the QVBoxLayout as the layout for the dialog, and displaying the dialog using self.exec()
.
QMainWindow with OOP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
from PySide6.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton class MyWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("My App") label = QLabel("Hello, GeeksCoders!") button = QPushButton("GeeksCoders") self.setCentralWidget(label) self.addToolBar("My toolbar").addWidget(button) self.show() app = QApplication([]) window = MyWindow() app.exec() |
In this example, we create a MyWindow
class that inherits from QMainWindow
. The __init__
method is used to initialize the window by setting the window title, creating a QLabel and a QPushButton, setting the QLabel as the central widget for the window, and adding the button to a toolbar using self.addToolBar().addWidget()
. Finally, the show()
method is called to display the window.
By using OOP with PySide6, we can create reusable and maintainable code that is easy to read and understand. It also allows us to encapsulate the logic and behavior of the window or dialog within its own class, making it easier to manage and modify in the future.