How to add a menu bar in PyQt5?
To add a menu bar in PyQt5, you need to use the QMenuBar class and the QMenu class. Here is a simple example:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QMenu
class MenuExample(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
menubar = self.menuBar()
fileMenu = menubar.addMenu('File')
newAction = QAction('New', self)
fileMenu.addAction(newAction)
openAction = QAction('Open', self)
fileMenu.addAction(openAction)
saveAction = QAction('Save', self)
fileMenu.addAction(saveAction)
exitAction = QAction('Exit', self)
exitAction.triggered.connect(self.close)
fileMenu.addAction(exitAction)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Menu Example')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MenuExample()
ex.show()
sys.exit(app.exec_())
In this example, we created a window class called MenuExample and added a menu bar that includes a menu called File with several actions (New, Open, Save, and Exit). When the Exit menu item is clicked, the window will close.
Running this code will display a window with a menu bar.
More tutorials
Using Classes in Tkinter Advanced Techniques(Opens in a new browser tab)
Progress Bar iOS also known as Progress View(Opens in a new browser tab)
The Best Approach for Reversing Strings in Python(Opens in a new browser tab)
How to create a cpp file in vscode?(Opens in a new browser tab)
Creating an Android ProgressBar utilizing Kotlin.(Opens in a new browser tab)PyQt5
To add a menu bar in PyQt5, you need to use the QMenuBar class and the QMenu class. Here is a simple example:
import sys
from PyQt5.QtWidgets import
QMainWindow
QMenuBar
QMenu