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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
/*Tool button styles */ QCalendarWidget QToolButton { height:40px; width:150px; color:white; font-size:20px; icon-size:56px,56px; background-color:black; } /* header row */ QCalendarWidget QWidget{ alternate-background-color:rgb(128,128,128) } /*normal days */ QCalendarWidget QAbstractItemView:enabled { font-size:18px; color:rgb(180,180,180); background-color:black; selection-background-color:rgb(64,64,64); selection-color:rgb(0,255,0); } |
Also you need to do the same for the QLabel.
1 2 3 4 5 6 |
QLabel { color:rgb(255, 0, 0) } |
This is our 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 LoadCalendar.py and add these codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
from PyQt5.QtWidgets import QApplication, QDialog, QCalendarWidget, QLabel import sys from PyQt5 import uic class UI(QDialog): def __init__(self): super().__init__() #this is used for loading ui file uic.loadUi('calendar.ui', self) #find the widgets self.calendar = self.findChild(QCalendarWidget, "calendarWidget") self.label = self.findChild(QLabel, "label") self.calendar.selectionChanged.connect(self.calendar_date) def calendar_date(self): dateselected = self.calendar.selectedDate() date_in_string = str(dateselected.toPyDate()) self.label.setText("Date Is : " + date_in_string) app = QApplication([]) window = UI() window.show() app.exec_() |
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.
1 2 3 4 5 |
def calendar_date(self): dateselected = self.calendar.selectedDate() date_in_string = str(dateselected.toPyDate()) self.label.setText("Date Is : " + date_in_string) |
Run the code and this is the result
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
from PyQt5.QtWidgets import QApplication, QDialog,\ QVBoxLayout, QCalendarWidget, QLabel import sys from PyQt5.QtGui import QIcon, QFont class Window(QDialog): def __init__(self): super().__init__() #window requrements like geometry,icon and title self.setGeometry(200,200,400,200) self.setWindowTitle("PyQt5 QCalendar") self.setWindowIcon(QIcon("python.png")) vbox = QVBoxLayout() self.calendar = QCalendarWidget() self.calendar.setGridVisible(True) self.calendar.selectionChanged.connect(self.calendar_date) self.label =QLabel("Hello") self.label.setFont(QFont("Sanserif", 15)) self.label.setStyleSheet('color:red') vbox.addWidget(self.calendar) vbox.addWidget(self.label) self.setLayout(vbox) def calendar_date(self): dateselected = self.calendar.selectedDate() date_in_string = str(dateselected.toPyDate()) self.label.setText("Date Is : " + date_in_string) App = QApplication(sys.argv) window = Window() window.show() sys.exit(App.exec()) |
These are the imports that we need, you can use QSpinBox class for creating of the spinbox in pyqt.
1 2 3 4 |
from PyQt5.QtWidgets import QApplication, QDialog,\ QVBoxLayout, QCalendarWidget, QLabel import sys from PyQt5.QtGui import QIcon, QFont |
You can use this code for setting the x,y position, width and height of the window.
1 |
self.setGeometry(200,200,400,200) |
This is the title for our window.
1 |
self.setWindowTitle("PyQt5 QCalendar") |
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.
1 |
self.setWindowIcon(QIcon("python.png")) |
You can create pyqt5 calendar by creating the object of QCalendarWidget.
1 |
self.calendar = QCalendarWidget() |
In here we have connected the selectionChanged() signal of the calendar with the calender_date() method that we create.
1 |
self.calendar.selectionChanged.connect(self.calendar_date) |
So now run the complete code and this is the result.