|
|
|
|
@ -0,0 +1,280 @@
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
日历组件模块
|
|
|
|
|
提供一个可嵌入的日历控件,用于在应用程序中显示和选择日期
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
from PyQt5.QtWidgets import (
|
|
|
|
|
QWidget, QCalendarWidget, QVBoxLayout, QHBoxLayout,
|
|
|
|
|
QPushButton, QLabel, QFrame
|
|
|
|
|
)
|
|
|
|
|
from PyQt5.QtCore import QDate, Qt, pyqtSignal
|
|
|
|
|
from PyQt5.QtGui import QFont
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CalendarWidget(QWidget):
|
|
|
|
|
"""日历组件类"""
|
|
|
|
|
|
|
|
|
|
# 自定义信号
|
|
|
|
|
date_selected = pyqtSignal(str) # 日期字符串信号,用于插入功能
|
|
|
|
|
|
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
|
super().__init__(parent)
|
|
|
|
|
self.setup_ui()
|
|
|
|
|
self.setup_connections()
|
|
|
|
|
|
|
|
|
|
def setup_ui(self):
|
|
|
|
|
"""设置UI界面"""
|
|
|
|
|
# 设置窗口属性
|
|
|
|
|
self.setWindowTitle("日历")
|
|
|
|
|
# 不再固定宽度,让其可以调整大小
|
|
|
|
|
|
|
|
|
|
# 设置白色背景
|
|
|
|
|
self.setStyleSheet("background-color: white;")
|
|
|
|
|
|
|
|
|
|
# 主布局
|
|
|
|
|
main_layout = QVBoxLayout()
|
|
|
|
|
main_layout.setContentsMargins(10, 10, 10, 10)
|
|
|
|
|
main_layout.setSpacing(10)
|
|
|
|
|
|
|
|
|
|
# 标题栏
|
|
|
|
|
title_layout = QHBoxLayout()
|
|
|
|
|
title_label = QLabel("日历")
|
|
|
|
|
title_label.setFont(QFont("Arial", 12, QFont.Bold))
|
|
|
|
|
title_layout.addWidget(title_label)
|
|
|
|
|
title_layout.addStretch()
|
|
|
|
|
|
|
|
|
|
# 关闭按钮
|
|
|
|
|
self.close_btn = QPushButton("×")
|
|
|
|
|
self.close_btn.setFixedSize(25, 25)
|
|
|
|
|
self.close_btn.setStyleSheet("""
|
|
|
|
|
QPushButton {
|
|
|
|
|
background-color: #f0f0f0;
|
|
|
|
|
border: 1px solid #ccc;
|
|
|
|
|
border-radius: 12px;
|
|
|
|
|
font-size: 16px;
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
}
|
|
|
|
|
QPushButton:hover {
|
|
|
|
|
background-color: #e0e0e0;
|
|
|
|
|
}
|
|
|
|
|
""")
|
|
|
|
|
title_layout.addWidget(self.close_btn)
|
|
|
|
|
|
|
|
|
|
main_layout.addLayout(title_layout)
|
|
|
|
|
|
|
|
|
|
# 分隔线
|
|
|
|
|
separator = QFrame()
|
|
|
|
|
separator.setFrameShape(QFrame.HLine)
|
|
|
|
|
separator.setFrameShadow(QFrame.Sunken)
|
|
|
|
|
main_layout.addWidget(separator)
|
|
|
|
|
|
|
|
|
|
# 日历控件
|
|
|
|
|
self.calendar = QCalendarWidget()
|
|
|
|
|
self.calendar.setGridVisible(True)
|
|
|
|
|
self.calendar.setVerticalHeaderFormat(QCalendarWidget.NoVerticalHeader)
|
|
|
|
|
self.calendar.setNavigationBarVisible(True)
|
|
|
|
|
|
|
|
|
|
# 设置样式
|
|
|
|
|
self.calendar.setStyleSheet("""
|
|
|
|
|
QCalendarWidget {
|
|
|
|
|
background-color: white;
|
|
|
|
|
border: 1px solid #ccc;
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
}
|
|
|
|
|
QCalendarWidget QToolButton {
|
|
|
|
|
height: 30px;
|
|
|
|
|
width: 80px;
|
|
|
|
|
color: #333;
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
background-color: #f0f0f0;
|
|
|
|
|
border: 1px solid #ccc;
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
}
|
|
|
|
|
QCalendarWidget QToolButton:hover {
|
|
|
|
|
background-color: #e0e0e0;
|
|
|
|
|
}
|
|
|
|
|
QCalendarWidget QMenu {
|
|
|
|
|
width: 150px;
|
|
|
|
|
left: 20px;
|
|
|
|
|
color: white;
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
background-color: rgb(64, 64, 64);
|
|
|
|
|
}
|
|
|
|
|
QCalendarWidget QSpinBox {
|
|
|
|
|
width: 80px;
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
background-color: #f0f0f0;
|
|
|
|
|
selection-background-color: rgb(64, 64, 64);
|
|
|
|
|
selection-color: white;
|
|
|
|
|
border: 1px solid #ccc;
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
}
|
|
|
|
|
QCalendarWidget QSpinBox::up-button {
|
|
|
|
|
subcontrol-origin: border;
|
|
|
|
|
subcontrol-position: top right;
|
|
|
|
|
width: 20px;
|
|
|
|
|
}
|
|
|
|
|
QCalendarWidget QSpinBox::down-button {
|
|
|
|
|
subcontrol-origin: border;
|
|
|
|
|
subcontrol-position: bottom right;
|
|
|
|
|
width: 20px;
|
|
|
|
|
}
|
|
|
|
|
QCalendarWidget QSpinBox::up-arrow {
|
|
|
|
|
width: 10px;
|
|
|
|
|
height: 10px;
|
|
|
|
|
}
|
|
|
|
|
QCalendarWidget QSpinBox::down-arrow {
|
|
|
|
|
width: 10px;
|
|
|
|
|
height: 10px;
|
|
|
|
|
}
|
|
|
|
|
QCalendarWidget QWidget {
|
|
|
|
|
alternate-background-color: #f0f0f0;
|
|
|
|
|
}
|
|
|
|
|
QCalendarWidget QAbstractItemView:enabled {
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
selection-background-color: #0078d7;
|
|
|
|
|
selection-color: white;
|
|
|
|
|
}
|
|
|
|
|
QCalendarWidget QWidget#qt_calendar_navigationbar {
|
|
|
|
|
background-color: #f8f8f8;
|
|
|
|
|
}
|
|
|
|
|
""")
|
|
|
|
|
|
|
|
|
|
main_layout.addWidget(self.calendar)
|
|
|
|
|
|
|
|
|
|
# 当前日期显示
|
|
|
|
|
self.date_label = QLabel()
|
|
|
|
|
self.date_label.setAlignment(Qt.AlignCenter)
|
|
|
|
|
self.date_label.setFont(QFont("Arial", 10))
|
|
|
|
|
self.date_label.setStyleSheet("QLabel { color: #666; }")
|
|
|
|
|
self.update_date_label()
|
|
|
|
|
|
|
|
|
|
main_layout.addWidget(self.date_label)
|
|
|
|
|
|
|
|
|
|
# 操作按钮
|
|
|
|
|
button_layout = QHBoxLayout()
|
|
|
|
|
|
|
|
|
|
self.today_btn = QPushButton("今天")
|
|
|
|
|
self.today_btn.setStyleSheet("""
|
|
|
|
|
QPushButton {
|
|
|
|
|
background-color: #0078d7;
|
|
|
|
|
color: white;
|
|
|
|
|
border: none;
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
padding: 5px 10px;
|
|
|
|
|
}
|
|
|
|
|
QPushButton:hover {
|
|
|
|
|
background-color: #005a9e;
|
|
|
|
|
}
|
|
|
|
|
""")
|
|
|
|
|
|
|
|
|
|
self.clear_btn = QPushButton("清除")
|
|
|
|
|
self.clear_btn.setStyleSheet("""
|
|
|
|
|
QPushButton {
|
|
|
|
|
background-color: #f0f0f0;
|
|
|
|
|
color: #333;
|
|
|
|
|
border: 1px solid #ccc;
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
padding: 5px 10px;
|
|
|
|
|
}
|
|
|
|
|
QPushButton:hover {
|
|
|
|
|
background-color: #e0e0e0;
|
|
|
|
|
}
|
|
|
|
|
""")
|
|
|
|
|
|
|
|
|
|
# 插入按钮
|
|
|
|
|
self.insert_btn = QPushButton("插入")
|
|
|
|
|
self.insert_btn.setStyleSheet("""
|
|
|
|
|
QPushButton {
|
|
|
|
|
background-color: #4CAF50;
|
|
|
|
|
color: white;
|
|
|
|
|
border: none;
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
padding: 5px 10px;
|
|
|
|
|
}
|
|
|
|
|
QPushButton:hover {
|
|
|
|
|
background-color: #45a049;
|
|
|
|
|
}
|
|
|
|
|
""")
|
|
|
|
|
|
|
|
|
|
button_layout.addWidget(self.today_btn)
|
|
|
|
|
button_layout.addWidget(self.insert_btn)
|
|
|
|
|
button_layout.addStretch()
|
|
|
|
|
button_layout.addWidget(self.clear_btn)
|
|
|
|
|
|
|
|
|
|
main_layout.addLayout(button_layout)
|
|
|
|
|
|
|
|
|
|
self.setLayout(main_layout)
|
|
|
|
|
|
|
|
|
|
def setup_connections(self):
|
|
|
|
|
"""设置信号连接"""
|
|
|
|
|
self.calendar.clicked.connect(self.on_date_selected)
|
|
|
|
|
self.today_btn.clicked.connect(self.on_today_clicked)
|
|
|
|
|
self.clear_btn.clicked.connect(self.on_clear_clicked)
|
|
|
|
|
self.close_btn.clicked.connect(self.on_close_clicked)
|
|
|
|
|
self.insert_btn.clicked.connect(self.on_insert_clicked)
|
|
|
|
|
|
|
|
|
|
def on_date_selected(self, date):
|
|
|
|
|
"""日期选择事件"""
|
|
|
|
|
self.update_date_label(date)
|
|
|
|
|
|
|
|
|
|
def on_today_clicked(self):
|
|
|
|
|
"""今天按钮点击事件"""
|
|
|
|
|
today = QDate.currentDate()
|
|
|
|
|
self.calendar.setSelectedDate(today)
|
|
|
|
|
self.update_date_label(today)
|
|
|
|
|
|
|
|
|
|
def on_clear_clicked(self):
|
|
|
|
|
"""清除按钮点击事件"""
|
|
|
|
|
self.calendar.setSelectedDate(QDate())
|
|
|
|
|
self.date_label.setText("未选择日期")
|
|
|
|
|
|
|
|
|
|
def on_close_clicked(self):
|
|
|
|
|
"""关闭按钮点击事件"""
|
|
|
|
|
self.hide()
|
|
|
|
|
|
|
|
|
|
def on_insert_clicked(self):
|
|
|
|
|
"""插入按钮点击事件"""
|
|
|
|
|
selected_date = self.calendar.selectedDate()
|
|
|
|
|
if selected_date.isValid():
|
|
|
|
|
# 发送信号,将选中的日期传递给主窗口
|
|
|
|
|
date_str = selected_date.toString("yyyy年MM月dd日 dddd")
|
|
|
|
|
self.date_selected.emit(date_str)
|
|
|
|
|
|
|
|
|
|
def update_date_label(self, date=None):
|
|
|
|
|
"""更新日期显示标签"""
|
|
|
|
|
if date is None:
|
|
|
|
|
date = self.calendar.selectedDate()
|
|
|
|
|
|
|
|
|
|
if date.isValid():
|
|
|
|
|
date_str = date.toString("yyyy年MM月dd日 (ddd)")
|
|
|
|
|
self.date_label.setText(f"选中日期: {date_str}")
|
|
|
|
|
else:
|
|
|
|
|
self.date_label.setText("未选择日期")
|
|
|
|
|
|
|
|
|
|
def get_selected_date(self):
|
|
|
|
|
"""获取选中的日期"""
|
|
|
|
|
return self.calendar.selectedDate()
|
|
|
|
|
|
|
|
|
|
def set_selected_date(self, date):
|
|
|
|
|
"""设置选中的日期"""
|
|
|
|
|
if isinstance(date, str):
|
|
|
|
|
date = QDate.fromString(date, "yyyy-MM-dd")
|
|
|
|
|
self.calendar.setSelectedDate(date)
|
|
|
|
|
self.update_date_label(date)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
from PyQt5.QtWidgets import QApplication
|
|
|
|
|
|
|
|
|
|
app = QApplication(sys.argv)
|
|
|
|
|
|
|
|
|
|
# 创建并显示日历组件
|
|
|
|
|
calendar = CalendarWidget()
|
|
|
|
|
calendar.show()
|
|
|
|
|
|
|
|
|
|
sys.exit(app.exec_())
|