PyQt - QCalendar Widget



Calender plays an important role in our daily tasks and it is a useful tool for effective time management, upcoming meetings, planning, etc.

In PyQt,QCalendar widget is a useful date picker control. It provides a month-based view. The user can select the date by the use of the mouse or the keyboard, the default being todays date. The calendars date range can also be stipulated.

Following are some utility methods of this class −

Sr.No. Methods & Description
1

setDateRange()

Sets the lower and upper date available for selection

2

setFirstDayOfWeek()

Determines the day of the first column in the calendar

The predefined day constants are −

  • Qt.Monday
  • Qt.Tuesday
  • Qt.Wednesday
  • Qt.Thursday
  • Qt.Friday
  • Qt.Saturday
  • Qt.Sunday
3

setMinimumDate()

Sets the lower date for selection

4

setMaximumDate()

Sets the upper date for selection

5

setSelectedDate()

Sets a QDate object as the selected date

6

showToday()

Shows the month of today

7

selectedDate()

Retrieves the selected date

8

setGridvisible()

Turns the calendar grid on or off

Example 1

The following example has a calendar widget and a label which displays the currently selected date. The complete code is as follows −

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QCalendarWidget, QLabel
from PyQt6.QtCore import QDate

class Example(QWidget):
   def __init__(self):
      super().__init__()
      self.initUI()

   def initUI(self):
      cal = QCalendarWidget(self)
      cal.setGridVisible(True)
      cal.move(20, 20)
      cal.clicked[QDate].connect(self.showDate)

      self.lbl = QLabel(self)
      date = cal.selectedDate()
      self.lbl.setText(date.toString())
      self.lbl.move(20, 200)

      self.setGeometry(100, 100, 300, 300)
      self.setWindowTitle('Calendar')
      self.show()

   def showDate(self, date):
      self.lbl.setText(date.toString())

def main():
   app = QApplication(sys.argv)
   ex = Example()
   sys.exit(app.exec())

if __name__ == '__main__':
   main()

Output

The above code produces the following output −

QCalendar Widget Output

Example 2

Following is an another demonstration of calendar using QCalendar class and its methods.

# importing libraries
from PyQt6.QtWidgets import *
from PyQt6 import QtCore, QtGui
from PyQt6.QtGui import *
from PyQt6.QtCore import *
import sys
class Window(QMainWindow):

   def __init__(self):
      super().__init__()

      # setting title
      self.setWindowTitle("Tutorialspoint")

      # setting geometry
      self.setGeometry(110, 110, 610, 410)

      # calling method
      self.UiComponents()

      # showing all the widgets
      self.show()

   # method for components
   def UiComponents(self):

      # creating a QCalendarWidget object
      calendar = QCalendarWidget(self)

      # setting geometry to the calendar
      calendar.setGeometry(50, 50, 400, 250)

# create an app
App = QApplication(sys.argv)

# create the instance of our Window
window = Window()

# start the app
sys.exit(App.exec())

Output

The above code produces the following output −

qcalender_example_two
Advertisements