Chapter 3 - Create an empty QMainWindow#

You can now think of presenting your data in a UI. A QMainWindow provides a convenient structure for GUI applications, such as a menu bar and status bar. The following image shows the layout that QMainWindow offers out-of-the box:

QMainWindow layout

In this case, let your application inherit from QMainWindow, and add the following UI elements:

  • A “File” menu to open a File dialog.

  • An “Exit” menu close the window.

  • A status message on the status bar when the application starts.

In addition, you can define a fixed size for the window or adjust it based on the resolution you currently have. In the following snippet, you will see how window size is defined based on available screen width (80%) and height (70%).


You can achieve a similar structure using other Qt elements like QMenuBar, QWidget, and QStatusBar. Refer the QMainWindow layout for guidance.

 1from PySide6.QtCore import Slot
 2from PySide6.QtGui import QAction, QKeySequence
 3from PySide6.QtWidgets import QMainWindow
 6class MainWindow(QMainWindow):
 7    def __init__(self):
 8        QMainWindow.__init__(self)
 9        self.setWindowTitle("Eartquakes information")
11        # Menu
12 = self.menuBar()
13        self.file_menu ="File")
15        # Exit QAction
16        exit_action = QAction("Exit", self)
17        exit_action.setShortcut(QKeySequence.Quit)
18        exit_action.triggered.connect(self.close)
20        self.file_menu.addAction(exit_action)
22        # Status Bar
23        self.status = self.statusBar()
24        self.status.showMessage("Data loaded and plotted")
26        # Window dimensions
27        geometry = self.screen().availableGeometry()
28        self.setFixedSize(geometry.width() * 0.8, geometry.height() * 0.7)

Try running the script to see what output you get with it.