Skip to content
Related Articles

Related Articles

PyQt5 QCalendarWidget – Setting Text to the selected date
  • Last Updated : 21 Jun, 2020

In this article we will see how we can set text to the selected date of QCalendarWidget. In order to add the text to the selected date we have to use paintCell method, it is virtual protected method and it can be used by overwriting the method since this method is called in paintEvent(). Below is how the text will look like

Implementation Steps :
1. Create Calendar class which inherits the QCalendarWidget
2. Inside the Calendar class overwrite the paintCell method
3. Create a main window
4. Add Calendar object inside the main window

Below is the Calendar class code

# QCalendarWidget Class
class Calendar(QCalendarWidget):

    def paintCell(self, painter, rect, date):
        super(Calendar, self).paintCell(painter, rect, date)

        # checking if date is selected date
        if date == self.selectedDate():

            # saving the painter
            painter.save()

            # creating a QFont object
            font = QFont()

            # setting pixel size of the font
            font.setPixelSize(11)

            # making font bold
            font.setBold(True)

            # making font italic
            font.setItalic(True)

            # setting font to the painter
            painter.setFont(font)
            
            # drawing text
            painter.drawText(
                rect.topLeft() + QPoint(10, 10),
                "{}".format("Geek"),
            )
            
            # restoring the painter
            painter.restore()

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
  
# QCalendarWidget Class
class Calendar(QCalendarWidget):
  
    def paintCell(self, painter, rect, date):
        super(Calendar, self).paintCell(painter, rect, date)
  
        # checking if date is selected date
        if date == self.selectedDate():
  
            # saving the painter
            painter.save()
  
            # creating a QFont object
            font = QFont()
  
            # setting pixel size of the font
            font.setPixelSize(11)
  
            # making font bold
            font.setBold(True)
  
            # making font italic
            font.setItalic(True)
  
            # setting font to the painter
            painter.setFont(font)
  
            # drawing text
            painter.drawText(
                rect.topLeft() + QPoint(10, 10),
                "{}".format("Geek"),
            )
  
            # restoring the painter
            painter.restore()
  
  
  
class Window(QMainWindow):
  
    def __init__(self):
        super().__init__()
  
        # setting title
        self.setWindowTitle("Python ")
  
        # setting geometry
        self.setGeometry(100, 100, 500, 400)
  
        # calling method
        self.UiComponents()
  
        # showing all the widgets
        self.show()
  
  
    # method for components
    def UiComponents(self):
  
        # creating a QCalendarWidget object
        # as Calendar class inherits QCalendarWidget
        self.calendar = Calendar(self)
  
        # setting cursor
        self.calendar.setCursor(Qt.PointingHandCursor)
  
        # setting size of the calendar
        self.calendar.resize(350, 240)
  
        # setting font to the calendar
        self.calendar.setFont(QFont('Times', 5))
  
        # move the calendar
        self.calendar.move(10, 10)
  
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

Output :

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 :