PyQt5 - QCalendarWidget

PyQt5 - QCalendarWidget

QCalendarWidget in PyQt5 is a widget that provides a monthly-based calendar view, allowing the user to select dates and navigate through months and years. It's a versatile widget useful for applications that require date inputs. Here's a basic guide on how to use QCalendarWidget in a PyQt5 application.

Importing Necessary Modules

First, you need to import the necessary modules:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QCalendarWidget, QVBoxLayout

Creating the Main Window Class

You can create a class for your main window that includes the QCalendarWidget:

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

        # Create Calendar Widget
        self.calendar = QCalendarWidget(self)

        # Optional: Set a specific date format, selection mode, etc.
        self.calendar.setGridVisible(True)

        # Layout
        layout = QVBoxLayout()
        layout.addWidget(self.calendar)

        self.setLayout(layout)

        self.setWindowTitle("Calendar Example")
        self.setGeometry(300, 300, 400, 300)

Initializing and Running the Application

Finally, initialize and run your application:

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit(app.exec_())

Additional Features

  • Date Selection: You can connect signals to respond to date selections. For example, self.calendar.clicked.connect(self.on_date_selected) can be used to trigger a function when a date is selected.
  • Customizing Appearance: The appearance of the calendar can be customized, such as setting the first day of the week, formatting for dates, etc.
  • Date Range: You can also set the minimum and maximum dates that can be selected.

Example Usage

Here is a complete example that includes a function to handle date selection:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QCalendarWidget, QVBoxLayout
from PyQt5.QtCore import QDate

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

        # Create Calendar Widget
        self.calendar = QCalendarWidget(self)
        self.calendar.setGridVisible(True)
        self.calendar.clicked.connect(self.on_date_selected)

        # Layout
        layout = QVBoxLayout()
        layout.addWidget(self.calendar)

        self.setLayout(layout)

        self.setWindowTitle("Calendar Example")
        self.setGeometry(300, 300, 400, 300)

    def on_date_selected(self, date):
        print(f"Selected Date: {date.toString()}")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit(app.exec_())

This script creates a simple PyQt5 window with a calendar widget. When a date is selected, it prints the selected date to the console. You can expand this by adding more functionality based on your application's needs.


More Tags

rows zurb-foundation text-extraction file-put-contents rhino-mocks events msdeploy struct android-shape aem

More Programming Guides

Other Guides

More Programming Examples