PyQt5 Tutorial: Creating Menus and Toolbars

In this PyQt5 Tutorial we are going to learn about Creating Menus and Toolbars, so PyQt5 is popular Python library used for creating desktop applications with graphical user interface (GUI). In this tutorial we are going to learn about creating menus and toolbars using PyQt5.

 

 

To follow this this tutorial, you will need to have PyQt5 installed on your computer. you can install it using pip in your terminal.

You will also need code editor or an integrated development environment (IDE) such as PyCharm or Visual Studio Code.

 

 

 

OK now first of all we need to create basic PyQt5 application that we will use to demonstrate how to create menus and toolbars. open your code editor or IDE and create new Python file, after that add this code.

In the above code we have imported  necessary modules from PyQt5 and defined a class named App which extends from QMainWindow. after that we have defined the constructor to set the window’s title, size and position. then we called the MyUI method, which sets the window’s title and geometry and displays it on the screen.

 

 

Run the complete code and this will be the result

PyQt5 Tutorial: Creating Menus and Toolbars
PyQt5 Tutorial: Creating Menus and Toolbars

 

 

 

Now that we have created our basic application, let’s add menu bar to our application. Menus are standard user interface element that allow users to perform different actions within an application. we can create menu bar by creating QMenuBar object and adding it to the main window.

In the above code we have created QMenuBar object and assigned it to the menu_bar variable. after that we have created QMenu object named file_menu and added it to the menu bar using the addMenu method.

 

 

Run the complete code and this will be the result

PyQt5 Tutorial: Creating Menus and Toolbars
PyQt5 Tutorial: Creating Menus and Toolbars

 

 

Learn More on Python

Leave a Comment