Home » PyQt6 QCalendarWidget Tutorial

PyQt6 QCalendarWidget Tutorial

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

The QCalendarWidget in PyQt6 is a widget for displaying and selecting dates from a calendar interface. It provides various customization options, including changing the display format, limiting the selectable range, and responding to user interactions.

This tutorial explains the basics of QCalendarWidget, demonstrates customization options, and includes several code examples.

1. Basics of QCalendarWidget

Key Features:

  • Allows date selection with a calendar interface.
  • Provides signals to respond to user interactions.
  • Supports customization of displayed content and appearance.

Importing QCalendarWidget

from PyQt6.QtWidgets import QApplication, QMainWindow, QCalendarWidget
from PyQt6.QtCore import QDate

2. Example 1: Simple Calendar

This example creates a simple calendar widget.

Code:

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QCalendarWidget

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Simple QCalendarWidget")

        calendar = QCalendarWidget()
        self.setCentralWidget(calendar)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())

Output:

  • A window with a basic calendar allowing date selection.

3. Example 2: Handling Date Selection

In this example, the selectionChanged signal is used to respond when the user selects a date.

Code:

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QCalendarWidget
from PyQt6.QtCore import QDate

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Handle Date Selection")

        self.calendar = QCalendarWidget()
        self.calendar.selectionChanged.connect(self.date_changed)

        self.setCentralWidget(self.calendar)

    def date_changed(self):
        selected_date = self.calendar.selectedDate()
        print(f"Selected Date: {selected_date.toString('yyyy-MM-dd')}")

app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())

Output:

  • The console logs the selected date each time the user selects a different date.

4. Example 3: Setting a Minimum and Maximum Date

You can limit the selectable dates by setting a range with setMinimumDate and setMaximumDate.

Code:

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QCalendarWidget
from PyQt6.QtCore import QDate

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Set Date Range")

        calendar = QCalendarWidget()

        # Set date range
        calendar.setMinimumDate(QDate(2023, 1, 1))
        calendar.setMaximumDate(QDate(2023, 12, 31))

        self.setCentralWidget(calendar)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())

5. Example 4: Customizing Date Formats

You can change the date display format using setDateTextFormat or customize the header with setHorizontalHeaderFormat and setVerticalHeaderFormat.

Code:

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QCalendarWidget
from PyQt6.QtCore import QDate

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Customize Date Formats")

        calendar = QCalendarWidget()

        # Change header format
        calendar.setHorizontalHeaderFormat(QCalendarWidget.HorizontalHeaderFormat.ShortDayNames)
        calendar.setVerticalHeaderFormat(QCalendarWidget.VerticalHeaderFormat.NoVerticalHeader)

        # Highlight a specific date
        date = QDate.currentDate()
        format = calendar.dateTextFormat(date)
        format.setBackground(Qt.GlobalColor.yellow)
        calendar.setDateTextFormat(date, format)

        self.setCentralWidget(calendar)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())

6. Example 5: Responding to Double-Clicks

You can handle double-clicks on the calendar using the activated signal.

Code:

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QCalendarWidget
from PyQt6.QtCore import QDate

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Double-Click Date")

        self.calendar = QCalendarWidget()
        self.calendar.activated.connect(self.date_activated)

        self.setCentralWidget(self.calendar)

    def date_activated(self, date):
        print(f"Double-clicked Date: {date.toString('yyyy-MM-dd')}")

app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())

7. Example 6: Marking Special Dates

You can visually distinguish special dates by applying a custom text format.

Code:

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QCalendarWidget
from PyQt6.QtCore import QDate
from PyQt6.QtGui import QTextCharFormat

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Mark Special Dates")

        calendar = QCalendarWidget()

        # Highlight special dates
        special_date = QDate(2023, 12, 25)
        format = QTextCharFormat()
        format.setForeground(Qt.GlobalColor.red)
        format.setBackground(Qt.GlobalColor.lightGray)
        calendar.setDateTextFormat(special_date, format)

        self.setCentralWidget(calendar)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())

8. Example 7: Navigating the Calendar Programmatically

You can navigate the calendar programmatically by setting a new date or moving to a month or year.

Code:

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QCalendarWidget
from PyQt6.QtCore import QDate

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Programmatic Navigation")

        self.calendar = QCalendarWidget()

        # Set current date
        self.calendar.setSelectedDate(QDate(2023, 7, 4))

        # Jump to a specific month/year
        self.calendar.showSelectedDate(QDate(2023, 12, 25))

        self.setCentralWidget(self.calendar)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())

Summary of Key Features:

  1. Date Range: Restrict user selection to specific dates.
  2. Custom Formatting: Highlight or distinguish dates with custom text or background formatting.
  3. Signals: Respond to user interactions with selectionChanged and activated signals.
  4. Navigation: Programmatically navigate to specific dates or months.

These examples demonstrate how to leverage the QCalendarWidget for various date-selection tasks, ranging from simple usage to advanced customization.

You may also like

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