Today is the last show of learning control --QCalendarWidget
I. Description
QCalendarWidget provides a monthly calendar based controls that allow the user to select a date, you can also look at the icon inside:
QCalendarWidget same based on a subclass of QWidget QDialog, unlike the foregoing includes a pop-up menu used, so a lot of time needs to be used in conjunction QDialog.
II. Function
1. The date range
QCalendarWidget.setMaximumDate(self, date: typing.Union[QtCore.QDate, datetime.date])
QCalendarWidget.setMinimumDate(self, date: typing.Union[QtCore.QDate, datetime.date])
QCalendarWidget.setDateRange(self, min: typing.Union[QtCore.QDate, datetime.date], max: typing.Union[QtCore.QDate, datetime.date])
After setting the range, if the date is beyond the scope of the corresponding date dimmed.
2. Date Edit
Dates can be changed by clicking the mouse, of course, also be achieved through the keyboard
QCalendarWidget.setDateEditEnabled()
The set default value is True, on the control keyboard can be used directly, the following effects can be
That is, you can use the keyboard to jump directly enter the date
In the process we can also enter the following code control waits for keyboard input time
QCalendarWidget.setDateEditAcceptDelay (Self, delay: int) # value of the delay is ms
Of course, the above values may be obtained
QCalendarWidget.isDateEditEnabled() -> bool
QCalendarWidget.dateEditAcceptDelay() -> int
You can also set the current time
self, date: typing.Union[QtCore.QDate, datetime.date]
We can return to the current date by a button.
from PyQt5.Qt import * import sys class Window(QWidget): def __init__(self): super().__init__() self.resize(500,300) self.UI_test() def UI_test(self): self.cw = QCalendarWidget(self) self.btn = QPushButton('test',self) self.btn.move(250,0) self.btn.clicked.connect(self.fun) def fun(self): date = QDate.currentDate() self.cw.setSelectedDate(date) pass if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
3. Date of acquisition
QCalendarWidget.monthShown()-> int QCalendarWidget.yearShown()-> int QCalendarWidget.selectedDate() -> QtCore.QDate
Note that the difference between the selection and display, select a date selected by the cursor (selected after changing the year / month but the selected date will not change, only the value of the variable display)
4. Appearance format
a. Navigation Bar
QCalendarWidget.setNavigationBarVisible()
QCalendarWidget.isNavigationBarVisible()
b. The first day of the week of
Monday is the first day of default, but the default is Sunday in some places abroad, you can follow the following methods to set
QCalendarWidget.setFirstDayOfWeek(self, dayOfWeek: QtCore.Qt.DayOfWeek) Monday = ... # type: 'Qt.DayOfWeek' Tuesday = ... # type: 'Qt.DayOfWeek' Wednesday = ... # type: 'Qt.DayOfWeek' Thursday = ... # type: 'Qt.DayOfWeek' Friday = ... # type: 'Qt.DayOfWeek' Saturday = ... # type: 'Qt.DayOfWeek' Sunday = ... # type: 'Qt.DayOfWeek'
c. Grid Display
Default calendar is not with a grid, the grid can be separated by date
QCalendarWidget.setGridVisible()
QCalendarWidget.isGridVisible() -> bool
The effect of the grid is so
d. Text Format
Vertical head, the head horizontally disposed
QCalendarWidget.setHeaderTextFormat(self, format: QtGui.QTextCharFormat)
Format level head
QCalendarWidget.setHorizontalHeaderFormat(self, format: 'QCalendarWidget.HorizontalHeaderFormat') # type: 'QCalendarWidget.HorizontalHeaderFormat' NoHorizontalHeader = ... # type: 'QCalendarWidget.HorizontalHeaderFormat' SingleLetterDayNames = ... # type: 'QCalendarWidget.HorizontalHeaderFormat' ShortDayNames = ... # type: 'QCalendarWidget.HorizontalHeaderFormat' LongDayNames = ... # type: 'QCalendarWidget.HorizontalHeaderFormat'
Vertical header format
QCalendarWidget.setVerticalHeaderFormat(self, format: 'QCalendarWidget.VerticalHeaderFormat') # type: 'QCalendarWidget.VerticalHeaderFormat' NoVerticalHeader = ... # 隐藏 ISOWeekNumbers = ... # 显示周数
Modify week fonts and font formatting date
QCalendarWidget.setDateTextFormat (Self, DATE: typing.Union [QtCore.QDate, datetime.date], Color: QtGui.QTextCharFormat) # You can modify the font specified day of the week QCalendarWidget.setDateTextFormat (self, date: typing.Union [ QtCore. QDate, datetime.date], Color: QtGui.QTextCharFormat) # can modify the specified date font
5. Select
QCalendarWidget.setSelectedDate (Self, DATE: typing.Union [QtCore.QDate, datetime.date]) QCalendarWidget.setSelectionMode (Self, MODE: ' QCalendarWidget.SelectionMode ' ) # Date Select Mode # type: 'QCalendarWidget.SelectionMode' NOSELECTION =. .. # date can not be selected SingleSelection = ... # only radio
You can not select mainly used to show users the date, and can not be changed.
III. Common methods
QCalendarWidget.showToday () # show day, is responsible only for display when the page is not responsible for selected QCalendarWidget.showSelectedDate () QCalendarWidget.showNextMonth () QCalendarWidget.showNextYear () QCalendarWidget.showPreviousMonth () QCalendarWidget.showPreviousYear () QCalendarWidget.setCurrentPage (Self, year : int, month: int)
Note that the show is only responsible for showing that a day, month or year without additional selected effect.
IV. Signal
QCalendarWidget.activated (Self, DATE: typing.Union [QtCore.QDate, datetime.date]) # Enter or double-click can trigger parameters to date QCalendarWidget.clicked (self, date: typing.Union [ QtCore.QDate, datetime .date]) # trigger when clicked, the parameters for the date QCalendarWidget.currentPageChanged (Self, year: int, month the: int) # current page to change the parameters for the new page (month and year) QCalendarWidget.selectionChanged () # selected the date change, no parameters
Click to select the trigger and the trigger there is a difference, click on the trigger when the trigger is only a mouse click, and select the trigger can be selected through code or keyboard input.