Adding application Help and About dialogs

Put some finishing touches to your Python GUI application

PyQt5 Tutorial Build a Web Browser with PyQt5

Heads up! You've already completed this tutorial.

In the previous parts we've added some basic UI elements, including menus and toolbars, and implemented basic loading & saving of HTML files to the browser view. Now, to complete the standard interface, we will add a Help menu with an About dialog using PyQt5.

Since our application is a web browser, it makes sense to show the help in the browser view. We have two options here: either include the help HTML with the application, and use our load HTML code from the previous tutorial. Or, load up a web page in the browser view.

Here we're doing the latter, redirecting the user to a web page (this tutorial!), but have a go at implementing loading an HTML file for custom offline help.

Creating the Help Menu with QAction

The first step is to create the Help menu and add QAction items for the About dialog and a homepage link. Each action gets an icon, status tip, and a connected slot method.

python
help_menu = self.menuBar().addMenu("&Help")

about_action = QAction(
    QIcon(os.path.join('icons','question.png')), "About Mozarella Ashbadger", self
)
about_action.setStatusTip("Find out more about Mozarella Ashbadger") # Hungry!
about_action.triggered.connect(self.about)
help_menu.addAction(about_action)

navigate_mozarella_action = QAction(
    QIcon(os.path.join('icons','lifebuoy.png')), "Mozarella Ashbadger Homepage", self
)
navigate_mozarella_action.setStatusTip("Go to Mozarella Ashbadger Homepage")
navigate_mozarella_action.triggered.connect(self.navigate_mozarella)
help_menu.addAction(navigate_mozarella_action)

Implementing the Slot Methods

Next we add two custom slot methods to handle the display of the dialog, and to load the "browser page" with more information.

The first method navigate_mozarella opens up a page with more information on the browser; the second creates and executes a custom QDialog class, AboutDialog.

python
def navigate_mozarella(self):
    self.browser.setUrl(
        QUrl("https://www.pythonguis.com/examples/python-web-browser/")
    )

def about(self):
    dlg = AboutDialog()
    dlg.exec_()

Building a Custom About Dialog with QDialog

The definition for the About dialog is given below. The structure follows that seen earlier, with a QDialogButtonBox and associated signals to handle user input, and a series of QLabels to display the application information and a logo.

The only trick here is adding all the elements to the layout, then iterating over them to set the alignment to the center in a single loop. This saves duplication for the individual sections.

python
class AboutDialog(QDialog):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        QBtn = QDialogButtonBox.Ok  # No cancel
        self.buttonBox = QDialogButtonBox(QBtn)
        self.buttonBox.accepted.connect(self.accept)
        self.buttonBox.rejected.connect(self.reject)

        layout = QVBoxLayout()

        title = QLabel("Mozarella Ashbadger")
        font = title.font()
        font.setPointSize(20)
        title.setFont(font)

        layout.addWidget(title)

        logo = QLabel()
        logo.setPixmap(QPixmap( os.path.join('icons','ma-icon-128.png')))
        layout.addWidget(logo)

        layout.addWidget(QLabel("Version 23.35.211.233232"))
        layout.addWidget(QLabel("Copyright 2015 Mozarella Inc."))

        for i in range(layout.count()):
            layout.itemAt(i).setAlignment(Qt.AlignHCenter)

        layout.addWidget(self.buttonBox)

        self.setLayout(layout)

This completes the basic user interface for our web browser. In the next part, we're going to take this functional web browser and extend it to implement tabbed web browsing -- allowing us to keep multiple documents open at the same time.

Have other improvements you'd like to make? Then do! It's the best way to learn.

1:1 Coaching & Tutoring for your Python GUIs project
Martin Fitzpatrick Python GUIs Coaching & Training
60 mins ($195) Book Now

1:1 Python GUIs Coaching & Training

Comprehensive code reviewBugfixes & improvements • Maintainability advice and architecture improvements • Design and usability assessment • Suggestions and tips to expand your knowledgePackaging and distribution help for Windows, Mac & Linux • Find out more.

PyQt/PySide Development Services — Stuck in development hell? I'll help you get your project focused, finished and released. Benefit from years of practical experience releasing software with Python.

Find out More

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak
Martin Fitzpatrick

Adding application Help and About dialogs was written by Martin Fitzpatrick with contributions from Leo Well.

Martin Fitzpatrick is the creator of Python GUIs, and has been developing Python/Qt applications for the past 12+ years. He has written a number of popular Python books and provides Python software development & consulting for teams and startups.