PyQt5 Tutorials
About Lesson

In this PyQt5 Tutorial we want to learn about PyQt5 QCalendarWidget, so QCalendarWidget  class provides a monthly based calendar widget allowing the user to select a date.

In this lesson we are going to create two examples on pyqt QCalendarWidget class, the first example is using Qt Designer and the second one is using coding.  you can just write pyqt5designer in your terminal, after opening the Qt Designer you need to create Dialog without Buttons window. now we add widgets in Qt Designer.

  • Add a QVBoxLayout in your Designer
  • Add a QCalendar Widget in QVBoxLayout
  • Add a QLabel in QVBoxLayout



OK now right click on the QCalendarWidget and select Change Stylesheet, because we want to add some styles to our pyqt calendar, and add these styles in their.



Also you need to do the same for the QLabel.




This is our design

PyQt5 QCalendar Design
PyQt5 QCalendar Design



Now save your ui file, iam going to name it calendar.ui, after that copy the file and paste it in your working directory. because we want to load our ui file, for loading ui file we want to use uic module, so create a new Python file, i want to name it and add these codes.


Also we need to find the child in our ui file using findChild() method, because we want to connect selectionChanged() signal of the QCalendarWidget with calendar_date() method.



This is the method that we have connected with the selectionChanged() signal of the QCalendarWidget, so in this method first we have got the date from Calendar and we have set the date in the QLabel.




Run the code and this is the result 

PyQt5 Calendar - Working with QCalendarWidget in PyQt
PyQt5 Calendar – Working with QCalendarWidget in PyQt



Creating PyQt Calendar with Coding

OK now we want to create our pyqt5 calendar using coding so create a new Python file and add this code.



These are the imports that we need, you can use QSpinBox class for creating of the spinbox in pyqt.



You can use this code for setting the x,y position, width and height of the window.



This is the title for our window.



If you want to set an icon for the window, than you can use this code, make sure that you have already added an icon in your working directory.



You can create pyqt5 calendar by creating the object of QCalendarWidget.



In here we have connected the selectionChanged() signal of the calendar with the calender_date() method that we create.




So now run the complete code and this is the result.

Working with QCalendarWidget in PyQt
Working with QCalendarWidget in PyQt