Skip to content
Related Articles

Related Articles

Improve Article

PyQt5 QCalendarWidget – Setting Custom Shortcuts to go Specific Month

  • Last Updated : 12 Jun, 2020

In this article we will see how we can set the custom shortcuts for the QCalendarWidget to go to the specific month for example when user press the ‘O’ key calendar should show the October similarly for other months as well. And for months like January, June and July who share same initial then when the ‘J’ key is pressed it should the next month to the current month for example if current month is June and ‘J’ key is pressed it should show the July month and if present month was July then it should show the January.

Implementation steps:
1. Create a main window
2. Create a QCalendarWidget
3. Set various properties to the calendar
4. Grab the keyboard of the calendar so that default keyboard function could not take place
5. Override the keyPressEvent
6. Inside the override method create a list of keys for all the months
7. Get the current month and year of the calendar
8. Check if the ‘J’ key is pressed then check if current month is January then set current month as June, if current month is June set current month July else set current month as January
9. Check if the ‘ M’ key is pressed then check if the current month is March then set current month as May else set current month as March
10 Similarly, check if the ‘A’ key is pressed then check if the current month is April set current month as August else set current month as April
11. If any other key is pressed check if the key is present in the month list then get the index of the key in the list then set current month as index + 1

Below is the implementation




# importing libraries
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import sys
  
  
class Window(QMainWindow):
  
    def __init__(self):
        super().__init__()
  
        # setting title
        self.setWindowTitle("Python ")
  
        # setting geometry
        self.setGeometry(100, 100, 650, 400)
  
        # calling method
        self.UiComponents()
  
        # showing all the widgets
        self.show()
  
    # method for components
    def UiComponents(self):
  
        # creating a QCalendarWidget object
        self.calendar = QCalendarWidget(self)
  
        # setting geometry to the calender
        self.calendar.setGeometry(50, 10, 400, 250)
  
        # setting cursor
        self.calendar.setCursor(Qt.PointingHandCursor)
  
        # grabbing the keyboard for the calendar
        # to stop the predefined action
        self.calendar.grabKeyboard()
  
  
    # overriding key release event
    def keyPressEvent(self, e):
  
  
        # creating a key list according to the month
        keylist = [Qt.Key_J, Qt.Key_F, Qt.Key_M, Qt.Key_A, Qt.Key_M, Qt.Key_J,
                   Qt.Key_J, Qt.Key_A, Qt.Key_S, Qt.Key_O, Qt.Key_N, Qt.Key_D]
  
        # getting current month
        month = self.calendar.monthShown()
  
        # getting current year
        year = self.calendar.yearShown()
  
        # when J key is pressed
        if e.key() == keylist[0]:
            print("J Key Pressed")
  
            # if current month is January
            if month == 1:
  
                # set month as June
                self.calendar.setCurrentPage(year, 6)
  
            # if current month is June
            elif month == 6:
  
                # set month as July
                self.calendar.setCurrentPage(year, 7)
  
            # if current month is not june or july
            else:
  
                # set month as January
                self.calendar.setCurrentPage(year, 1)
  
        # if M key is pressed
        elif e.key() == keylist[2]:
            print("M key pressed")
  
            # if current month is March
            if month == 3:
  
                # set month as May
                self.calendar.setCurrentPage(year, 5)
  
            # if current month is not May
            else:
                # set month as March
                self.calendar.setCurrentPage(year, 3)
  
        # if A key is pressed
        elif e.key() == keylist[3]:
            print("A key pressed")
  
            # if current month is April
            if month == 4:
  
                # set month as August
                self.calendar.setCurrentPage(year, 8)
  
            # if current month is not August
            else:
                # set month as April
                self.calendar.setCurrentPage(year, 4)
  
        # if any other key is pressed
        elif e.key() in keylist:
  
            # get the position of the key in the list
            index = keylist.index(e.key())
            print(str(index + 1) + " Month Key Pressed")
  
            # set the month
            self.calendar.setCurrentPage(year, index + 1 )
  
  
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())


Output :
A key pressed
A key pressed
J Key Pressed
J Key Pressed
J Key Pressed
10 Month Key Pressed
12 Month Key Pressed
2 Month Key Pressed
11 Month Key Pressed
J Key Pressed
A key pressed
J Key Pressed
A key pressed
J Key Pressed
10 Month Key Pressed

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :