Skip to content
Related Articles

Related Articles

PyQt5 QCalendarWidget – Setting Key Release Event
  • Last Updated : 12 Jun, 2020
GeeksforGeeks - Summer Carnival Banner

In this article we will see how we can implement the key release event for the QCalendarWidget. In order to set the key release event we have to override the keyReleaseEvent method, by overriding the key release event we can add functions to the calendar whenever the pressed key is released. Unlike the key press event, key release event take place when the pressed key get released we can say that first key press event take place then the release event take place

Implementation steps:
1. Create a main window
2. Create a QCalendarWidget
3. Set various properties to the calendar
4. Override the keyReleaseEvent
5. Inside the override method check if the escape key pressed then hide the calendar

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)
  
  
    # overriding key release event
    def keyReleaseEvent(self, e):
  
        # when escape key is released
        if e.key() == Qt.Key_Escape:
  
            # hide the calendar
            self.calendar.hide()
            print("Escape key released Hide the calendar")
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

Output :

Escape key released Hide the calendar

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :