Social Icons

Tuesday, January 18, 2022

Create a menu with QMenuBar PyQt5

1 - Simple menu with QMenuBar PyQt5

To create a menu, the PyQt5 library has a class named
QMenuBar
which allows you to create a menu bar. By making an instantiation on the
QMenuBar

class, we create a menuBar object with the methods: 

        1 - addMenu(): allowing to add a menu like File , Edit, Option...
        2 - addAction()
: allowing to add commands to menu items like: File->Open , File->Save , File->Save As , File->Exit,...
        3 - addSeparator():
allowing to add a separator between the menu elements

Example


from PyQt5.QtWidgets import QApplication, QWidget,QMenuBar
import sys 

app = QApplication(sys.argv)
root = QWidget()
root.setWindowTitle("Simple QMenu PyQt5")
root.setGeometry(100 , 100 , 500 , 300)

# create a Menubar by instanciating the QMenuBar class
menuBar = QMenuBar(root)
menuBar.setGeometry(0,0, 500, 25)

# create a menu called 'File'
File = menuBar.addMenu('File')

# add actions to the menu 'File' 
File.addAction('New')
File.addAction('Open' )
File.addAction('Save')
File.addAction('Save As')
File.addAction('Exit')
root.show()
sys.exit(app.exec_())



We can also add command action to the action menu for example when we click on the File->Open, a new window appears, when we click on the File->Exit we exit the window...

Example


from PyQt5.QtWidgets import QApplication, QWidget,QMenuBar, QAction
import sys , os

def exit():
    root.close()

def new():
    os.popen("python simplemenu.py")


app = QApplication(sys.argv)
root = QWidget()
root.setWindowTitle("Simple QMenu PyQt5")
root.setGeometry(100 , 100 , 500 , 300)

# create a Menubar by instanciating the QMenuBar class
menuBar = QMenuBar(root)
menuBar.setGeometry(0,0, 500, 25)

# create a menu called 'File'
File = menuBar.addMenu('File')

# add actions to the menu 'File' 
File.addAction('New' , new)
File.addAction('Open' )
File.addAction('Save')
File.addAction('Save As')
File.addAction('Exit' , exit)
root.show()
sys.exit(app.exec_())

2 - QMenuBar according to the object approach PyQt5

In order to write a clean code, we can also write previous code by using the python class:
from PyQt5.QtWidgets import QApplication, QWidget,QMenuBar
import sys, os

class MyWindow(QWidget):

    def __init__(self, win):
        super().__init__()
        self.win = win

    def exit(self):
        self.win.close()    
    

    def new(self):
        os.popen("python object1.py")

    def build(self):
        self.win.setWindowTitle("Example QMenu PyQt5")
        self.win.setGeometry(100 , 100 , 500 , 300)
        self.menubar = QMenuBar(self.win)
        self.File = self.menubar.addMenu('File')
        self.File.addAction('New' , self.new)
        self.File.addAction('Exit' , self.exit)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    root = QWidget()
    mywin = MyWindow(root)
    mywin.build()
    
    root.show()
    sys.exit(app.exec_())

3 - Add an action to a menu item using the QAction class

Now we will see another method of adding an action to a menu item using the QAction class. For this purpose the following steps must be followed:
    1 - we create the action using QAction class
    2 - We add the action to the menu using the addAction() method
    3 - We connect the action to the method that performs the action using the triggered.connect() method

# create an action with QAction class
exitAction = QAction('&Exit' , self)

# adding action to the menu File
self.File.addAction(exitAction)

# adding a shrtcut to the action
exitAction.setShortcut('Ctrl+Q')

# connecting to the method that perform the action
exitAction.triggered.connect(self.exit)

Full code

from PyQt5.QtWidgets import QApplication, QWidget,QMenuBar, QAction
import sys, os

class MyWindow(QWidget):

    def __init__(self, win):
        super().__init__()
        self.win = win

    def exit(self):
        self.win.close()    
    

    def new(self):
        os.popen("python object1.py")

    def build(self):
        self.win.setWindowTitle("Example QMenu PyQt5")
        self.win.setGeometry(100 , 100 , 500 , 300)
        self.menubar = QMenuBar(self.win)
        self.File = self.menubar.addMenu('File')

        # create an action with QAction class
        exitAction = QAction('&Exit' , self)

        # adding action to the menu File
        self.File.addAction(exitAction)

        # adding a shrtcut to the action
        exitAction.setShortcut('Ctrl+Q')
        
        # connecting to the method that perform the action
        exitAction.triggered.connect(self.exit)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    root = QWidget()
    mywin = MyWindow(root)
    mywin.build()
    
    root.show()
    sys.exit(app.exec_())


Younes Derfoufi
my-courses.net

No comments:

Post a Comment

Learn Python For Beginners

Category Of Mobile Courses

Actualités (644) Adsense (1) Affiliation (1) Algebraic Topology (2) Algorithmic (1) all-news (30) Android (5) Android App (8) Android app without code (4) Android Apps (256) Android Development (4) Android download (2) Android OS (3) AngularJS (1) Automata theory and formal language (5) Bootstrap CSS (1) C programming (5) Category and Functor (8) CMS (3) Computer Glossary (18) Create Mobile App With Ionic Framework (2) CSS (2) CSS-Cascading-Style-Sheets (4) Developpement Java (13) Differential Geometry (1) Django-Python-Framework (15) dropshiping (26) Earn Money by Internet (4) Emplois (23) Framework php (2) Fraud (2) Github (2) HTML (9) IT News (3) Java For Beginners (10) Javascript (12) Kotlin Programming Language (8) Kotlin For Mobile Android (1) Linux Download (2) Marketing (5) Mobile (3) Mobile Courses (4) Mobile Marketing (4) MoneyGram (1) News (721) Node.js (5) Open Source (1) Photoshop (1) Protect Computer (1) Python (37) Python BeautifulSoup (1) Python For Data Science (2) Python PyQt (13) Python Reference (1) Python Source Code (10) Python-Books (6) Python-DVD-Training (1) Python-Exercises (305) Python-Framework (1) Python-IDE (1) Python-Kivy-Framework (2) Python-Modules (1) Python-pdf (2) Python-pyQt (1) python-temp (3) Référencement (2) Script PHP (2) Security (6) SEO (1) Snipping Tool: Faq (1) Social Networks (1) Source Code (4) Statistics With SPSS (2) Surveillance Software (1) Travail à domicile (6) Tutoriels php en vidéos (2) Tutoriels-MySql (6) tutoriels-php (19) Utilitaires (1) VPS (1) Web Hosting (1) Webcam (1) Webmarketing (11) Western Union (1) Windows 10 (1) Windows 7 (4) Windows 7 Faq (2) Windows 8 (1) Windows Accessories (1) Windows Download (8) Windows Drivers (1) Windows Fonts (1) Windows Power Shell (2) Windows Registry (2) Windows Security (18) Windows Software (2) Windows Spyware (2) Windows utilities (3) Windows Virus (2) Windows Vista (3) Windows Wireless (1) Windows xp (1) Wordpress (1)
 

Sample text

Sample Text

 
Blogger Templates