📜  PyQt5 QCalendarWidget – 设置选择模式(1)

📅  最后修改于: 2023-12-03 15:03:57.331000             🧑  作者: Mango

PyQt5 QCalendarWidget – Setting Selection Mode

PyQt5 QCalendarWidget is a pre-built calendar widget that provides a fully-functional calendar for the user interface of PyQt5 applications. The selection mode determines how the user can select dates in the calendar. PyQt5 QCalendarWidget provides four types of selection modes: NoSelection, SingleSelection, MultiSelection and ExtendedSelection.

NoSelection

This is the default selection mode of the PyQt5 QCalendarWidget where the user cannot select a date through the widget interface. NoSelection is an ideal selection mode if you just want the user to view the calendar and not make any selections.

SingleSelection

SingleSelection selection mode allows the user to select a single date at a time. Once a date is selected, any previously selected date is unselected automatically.

    # Setting single selection mode
    calendar = QCalendarWidget(self)
    calendar.setSelectionMode(QCalendarWidget.SingleSelection)
MultiSelection

MultiSelection selection mode allows the user to select multiple dates at a time. The previously selected dates are not unselected when a new date is selected. To select multiple dates, the user needs to hold the Ctrl key while clicking on the desired dates.

    # Setting multi selection mode
    calendar = QCalendarWidget(self)
    calendar.setSelectionMode(QCalendarWidget.MultiSelection)
ExtendedSelection

ExtendedSelection selection mode also allows the user to select multiple dates at a time. The difference between MultiSelection and ExtendedSelection is that ExtendedSelection allows the user to select a range of dates by holding down the Shift key and clicking on the first and last dates in the range.

    # Setting extended selection mode
    calendar = QCalendarWidget(self)
    calendar.setSelectionMode(QCalendarWidget.ExtendedSelection)

In conclusion, the selection mode of PyQt5 QCalendarWidget determines how the user can select dates in the calendar. By default, the selection mode is NoSelection but can be changed to SingleSelection, MultiSelection, or ExtendedSelection.