Home » PyQt QCalendarWidget class

PyQt QCalendarWidget class

Oracle Java Certification
1 Year Subscription
Java SE 11 Developer (Upgrade) [1Z0-817]
Spring Framework Basics Video Course
Java SE 11 Programmer II [1Z0-816] Practice Tests
Java SE 11 Programmer I [1Z0-815] Practice Tests

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

Table of Contents

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 −

  • Qt.Monday
  • Qt.Tuesday
  • Qt.Wednesday
  • Qt.Thursday
  • Qt.Friday
  • Qt.Saturday
  • Qt.Sunday
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

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More