In this lesson we will create a Months menu with menu items from January to December. The most important take away in this lesson is learning how to create toggleable mean items, which gives you more control of your application.





Source Code

import sys
import calendar
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction

class AppDemo(QMainWindow):
    def __init__(self):
        super().__init__()
        self.resize(500, 500)

        menuMonths = self.menuBar().addMenu('Months')
        menuMonths.triggered.connect(self.printValue)

        for i in range(1, 13):
            action = menuMonths.addAction(calendar.month_name[i])
            action.setCheckable(True)

    def printValue(self, action):
        print('{0}-{1}'.format(action.text(), action.isChecked()))

app = QApplication(sys.argv)        

demo = AppDemo()
demo.show()

sys.exit(app.exec_())