Pyside2 Create MenuBar & MenuItems

In this Pyside2 article iam going to show you how to Create MenuBar & MenuItems in Pyside2. also we are going to learn that how you can set icons for the menu items and how you can use signal and slot mechanism for this. so a menu bar consists of a list of pull-down menu items. You add menu items with addMenu().

 

Also you can check my previous articles on Pyside2 GUI Development.

 

1: Getting Started With Pyside2 | Qt For Python 

2: Pyside2 GUI Creating First Window 

3: Pyside2 GUI Creating Window Icon

4: Pyside2 GUI How To Create Icon Modes

5: Pyside2 GUI How To Create Tooltip

6: Pyside2 GUI QPushButton With Signal And Slot

7: Pyside2 GUI Making Center The Window 

8: Python GUI How To Create AboutBox

9: Python GUI How to Create Digital Clock in Pyside2

10: How To Create StatusBar In Pyside2 

11: Pyside2 Creating QProgressBar

12: Pyside2 Layout Managment with QHBoxLayout

13: Pyside2 GridLayout Example

14: Pyside2 Creating Calendar 

15: Pyside2 Create CheckBox

16: Pyside2 Creating FontComboBox

17: Pyside2 Creating QCompleter 

18: Pyside2 GUI Creating Slider 

 

 

 

So now this is the complete code for Pyside2 Create MenuBar & MenuItems

 

 

 

So at the top first we have  imported our required classed from pyside2, after that we have created our window class that extends from QMainWindow, and we have added the requirement of our window in that class, also we have called our setIcon() and create_menu() methods in our class.

 

 

 

 

In here we have created menuBar object and we have added some items to the menuBar.

 

 

 

OK now in here we have added some menu items with the icons and also shortcuts. make sure that you have some icons in your working directory. we are using QAction for this purpose, so in applications many common commands can be invoked via menus, toolbar buttons, and keyboard shortcuts. Since  the user expects each command to be performed in the same way, regardless of the user interface used, it is useful to represent each command as an action. Actions can be added to menus and toolbars, and will automatically keep them in sync. For example, in a word processor, if the user presses a Bold toolbar button, the Bold menu item will automatically be checked.

 

 

 

In here we are going to connect triggered signal of menu item with the slot or method that we will create.

 

 

 

Now after creation of menu actions, we need to add that to the related menubar like this. basically in this example we want to add the menu items in our file menu.

 

 

 

And this is the method that we have already connected with triggered signal of menu item. this method is just for closing of the window.

 

 

Also every Pyside2 application must create an application object. The sys.argv parameter is a list of arguments from a command line.

 

 

 

Finally, we enter the mainloop of the application. The event handling starts from this point. The mainloop receives events from the window system and dispatches them to the application widgets. also we have created the object of our window class in here.

 

 

 

 

Run the complete code and this will be the result

 

Pyside2 Create MenuBar & MenuItems
Pyside2 Create MenuBar & MenuItems

 

 

 

 

Also you can watch the complete video for this article

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×