2024-01-09 23:55:39 +08:00
|
|
|
import time
|
|
|
|
|
2024-01-10 20:03:04 +08:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from PyQt5.QtCore import QTimer, QThread, pyqtSignal, Qt
|
|
|
|
from PyQt5.QtGui import QIcon
|
|
|
|
from PyQt5.QtWidgets import QApplication, QDialog, QCheckBox, QMessageBox, QCalendarWidget, QWidget, QVBoxLayout, \
|
|
|
|
QToolButton
|
|
|
|
|
|
|
|
from app.ui.Icon import Icon
|
2024-01-09 23:55:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CalendarDialog(QDialog):
|
|
|
|
selected_date_signal = pyqtSignal(int)
|
2024-01-10 00:03:31 +08:00
|
|
|
|
2024-01-09 23:55:39 +08:00
|
|
|
def __init__(self, date_range=None, parent=None):
|
|
|
|
"""
|
|
|
|
|
2024-01-10 00:03:31 +08:00
|
|
|
@param date_range: tuple[Union[QDate, datetime.date],Union[QDate, datetime.date]] #限定的可选择范围
|
2024-01-09 23:55:39 +08:00
|
|
|
@param parent:
|
|
|
|
"""
|
|
|
|
super().__init__(parent)
|
2024-01-10 00:03:31 +08:00
|
|
|
self.setWindowTitle('选择日期')
|
2024-01-09 23:55:39 +08:00
|
|
|
self.calendar = QCalendarWidget(self)
|
|
|
|
self.calendar.clicked.connect(self.onDateChanged)
|
2024-01-10 20:03:04 +08:00
|
|
|
prev_btn = self.calendar.findChild(QToolButton, "qt_calendar_prevmonth")
|
|
|
|
prev_btn.setIcon(Icon.Arrow_left_Icon)
|
|
|
|
next_btn = self.calendar.findChild(QToolButton, "qt_calendar_nextmonth")
|
|
|
|
next_btn.setIcon(Icon.Arrow_right_Icon)
|
|
|
|
self.date_range = date_range
|
2024-01-09 23:55:39 +08:00
|
|
|
if date_range:
|
|
|
|
self.calendar.setDateRange(*date_range)
|
2024-01-10 20:03:04 +08:00
|
|
|
# 从第一天开始,依次添加日期到列表,直到该月的最后一天
|
|
|
|
current_date = date_range[1]
|
|
|
|
while (current_date + timedelta(days=1)).month == date_range[1].month:
|
|
|
|
current_date += timedelta(days=1)
|
|
|
|
range_format = self.calendar.dateTextFormat(current_date)
|
|
|
|
range_format.setForeground(Qt.gray)
|
|
|
|
self.calendar.setDateTextFormat(current_date, range_format)
|
|
|
|
# 从第一天开始,依次添加日期到列表,直到该月的最后一天
|
|
|
|
current_date = date_range[0]
|
|
|
|
while (current_date - timedelta(days=1)).month == date_range[0].month:
|
|
|
|
current_date -= timedelta(days=1)
|
|
|
|
range_format = self.calendar.dateTextFormat(current_date)
|
|
|
|
range_format.setForeground(Qt.gray)
|
|
|
|
self.calendar.setDateTextFormat(current_date, range_format)
|
2024-01-09 23:55:39 +08:00
|
|
|
layout = QVBoxLayout(self)
|
|
|
|
layout.addWidget(self.calendar)
|
|
|
|
self.setLayout(layout)
|
|
|
|
|
2024-01-10 20:03:04 +08:00
|
|
|
def set_start_date(self):
|
|
|
|
if self.date_range:
|
|
|
|
self.calendar.setCurrentPage(self.date_range[0].year, self.date_range[0].month)
|
|
|
|
def set_end_date(self):
|
|
|
|
if self.date_range:
|
|
|
|
self.calendar.setCurrentPage(self.date_range[1].year, self.date_range[1].month)
|
2024-01-09 23:55:39 +08:00
|
|
|
def onDateChanged(self):
|
|
|
|
# 获取选择的日期
|
|
|
|
selected_date = self.calendar.selectedDate()
|
|
|
|
s_t = time.strptime(selected_date.toString("yyyy-MM-dd"), "%Y-%m-%d") # 返回元祖
|
|
|
|
mkt = int(time.mktime(s_t))
|
|
|
|
timestamp = mkt
|
|
|
|
self.selected_date_signal.emit(timestamp)
|
2024-01-10 00:03:31 +08:00
|
|
|
print("Selected Date:", selected_date.toString("yyyy-MM-dd"), timestamp)
|
2024-01-09 23:55:39 +08:00
|
|
|
self.close()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
2024-01-10 20:03:04 +08:00
|
|
|
from datetime import datetime, timedelta
|
2024-01-10 00:03:31 +08:00
|
|
|
|
2024-01-09 23:55:39 +08:00
|
|
|
app = QApplication(sys.argv)
|
|
|
|
# 设置日期范围
|
2024-01-10 20:03:04 +08:00
|
|
|
start_date = datetime(2024, 1, 5)
|
2024-01-09 23:55:39 +08:00
|
|
|
end_date = datetime(2024, 1, 9)
|
2024-01-10 00:03:31 +08:00
|
|
|
date_range = (start_date.date(), end_date.date())
|
2024-01-09 23:55:39 +08:00
|
|
|
ex = CalendarDialog(date_range=date_range)
|
|
|
|
ex.show()
|
|
|
|
sys.exit(app.exec_())
|