903
In this article we will look at the PyQt QCalendarWidget class .
The QCalendarWidget displays a calendar for the users to select a date.
Lets look at some of the methods that are available for the QCalendarWidget class
Methods
Method | Description |
---|---|
setDateRange() | Sets the lower and upper date available for selection |
setFirstDayOfWeek() | Determines the day of the first column in the calendar
The predefined day constants are −
|
setMinimumDate() | Sets the lower date for selection |
setMaximumDate() | Sets the upper date for selection |
setSelectedDate() | Sets a QDate object as the selected date |
showToday() | Shows the month of today |
selectedDate() | Retrieves the selected date |
setGridvisible() | Turns the calendar grid on or off |
Examples
Lets look at a basic example
import sys from PyQt5.QtWidgets import (QApplication, QWidget, QLabel, QVBoxLayout, QCalendarWidget) from PyQt5.QtCore import QDate class MyApp(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): calendar = QCalendarWidget(self) calendar.setGridVisible(True) calendar.clicked[QDate].connect(self.showDate) self.lbl = QLabel(self) date = calendar.selectedDate() self.lbl.setText(date.toString()) vbox = QVBoxLayout() vbox.addWidget(calendar) vbox.addWidget(self.lbl) self.setLayout(vbox) self.setWindowTitle('QCalendarWidget Example') self.setGeometry(300, 300, 400, 300) self.show() def showDate(self, date): self.lbl.setText(date.toString()) if __name__ == '__main__': app = QApplication(sys.argv) ex = MyApp() sys.exit(app.exec_())
This displayed the following