|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
@FishC
- import sys
- from PyQt6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
- QLabel, QCalendarWidget, QPushButton, QToolButton)
- from PyQt6.QtCore import Qt, QDate, QSize
- from PyQt6.QtGui import (QColor, QTextCharFormat, QFont, QPainter,
- QPalette, QPen, QBrush, QIcon, QPixmap)
- class RoundCellCalendar(QCalendarWidget):
- def __init__(self, parent=None):
- super().__init__(parent)
- self.setStyleSheet("""
- QCalendarWidget {
- border: 2px solid #e0e0e0;
- border-radius: 15px;
- padding: 5px;
- background: white;
- }
- /* 导航按钮容器 */
- QCalendarWidget QWidget#qt_calendar_navigationbar {
- background: #f5f5f5;
- border-top-left-radius: 13px;
- border-top-right-radius: 13px;
- padding: 5px;
- }
- /* 月份年份标签 */
- QCalendarWidget QToolButton#qt_calendar_monthbutton {
- font-size: 14px;
- font-weight: bold;
- color: #444444;
- padding: 5px 10px;
- border: none;
- }
- /* 年份选择框 */
- QCalendarWidget QSpinBox {
- width: 80px;
- font-size: 14px;
- border-radius: 5px;
- padding: 2px;
- border: 1px solid #d0d0d0;
- }
- /* 月份选择菜单 */
- QCalendarWidget QMenu {
- background: white;
- border-radius: 10px;
- padding: 5px;
- border: 1px solid #d0d0d0;
- }
- /* 选中日期的默认样式覆盖 */
- QCalendarWidget QAbstractItemView:enabled {
- selection-background-color: transparent;
- selection-color: #333333;
- }
- """)
-
- # 自定义导航按钮样式
- self.setup_navigation_buttons()
-
- # 初始化日期格式
- self.init_date_formats()
-
- def setup_navigation_buttons(self):
- # 获取导航栏中的按钮
- prev_btn = self.findChild(QToolButton, "qt_calendar_prevmonth")
- next_btn = self.findChild(QToolButton, "qt_calendar_nextmonth")
-
- if prev_btn and next_btn:
- # 设置按钮样式
- button_style = """
- QToolButton {
- background: #f0f0f0;
- border-radius: 12px;
- border: 1px solid #d0d0d0;
- width: 24px;
- height: 24px;
- }
- QToolButton:hover {
- background: #e0e0e0;
- }
- QToolButton:pressed {
- background: #d0d0d0;
- }
- """
- prev_btn.setStyleSheet(button_style)
- next_btn.setStyleSheet(button_style)
-
- # 创建并设置自定义箭头图标
- arrow_size = QSize(16, 16)
-
- # 左箭头图标
- prev_icon = QIcon()
- prev_pixmap = QPixmap(arrow_size)
- prev_pixmap.fill(Qt.GlobalColor.transparent)
- painter = QPainter(prev_pixmap)
- painter.setRenderHint(QPainter.RenderHint.Antialiasing)
- painter.setPen(QPen(QColor(80, 80, 80), 2))
- painter.drawLine(12, 8, 4, 8)
- painter.drawLine(5, 8, 9, 4)
- painter.drawLine(5, 8, 9, 12)
- painter.end()
- prev_icon.addPixmap(prev_pixmap)
- prev_btn.setIcon(prev_icon)
- prev_btn.setIconSize(arrow_size)
-
- # 右箭头图标
- next_icon = QIcon()
- next_pixmap = QPixmap(arrow_size)
- next_pixmap.fill(Qt.GlobalColor.transparent)
- painter = QPainter(next_pixmap)
- painter.setRenderHint(QPainter.RenderHint.Antialiasing)
- painter.setPen(QPen(QColor(80, 80, 80), 2))
- painter.drawLine(4, 8, 12, 8)
- painter.drawLine(11, 8, 7, 4)
- painter.drawLine(11, 8, 7, 12)
- painter.end()
- next_icon.addPixmap(next_pixmap)
- next_btn.setIcon(next_icon)
- next_btn.setIconSize(arrow_size)
-
- def init_date_formats(self):
- # 今天的样式 (绿色圆角)
- today_fmt = QTextCharFormat()
- today_fmt.setBackground(QColor(230, 245, 230)) # 浅绿色背景
- today_fmt.setForeground(QColor(0, 100, 0)) # 深绿色文字
- today_fmt.setFont(QFont("Arial", 10, QFont.Weight.Bold))
- self.setDateTextFormat(QDate.currentDate(), today_fmt)
-
- # 周末的样式 (柔和的红色)
- weekend_fmt = QTextCharFormat()
- weekend_fmt.setForeground(QColor(220, 50, 50)) # 柔和的红色
-
- # 工作日的样式 (深灰色)
- weekday_fmt = QTextCharFormat()
- weekday_fmt.setForeground(QColor(60, 60, 60)) # 深灰色文字
-
- # 设置周末和工作日样式
- self.setWeekdayTextFormat(Qt.DayOfWeek.Saturday, weekend_fmt)
- self.setWeekdayTextFormat(Qt.DayOfWeek.Sunday, weekend_fmt)
-
- for day in [Qt.DayOfWeek.Monday, Qt.DayOfWeek.Tuesday,
- Qt.DayOfWeek.Wednesday, Qt.DayOfWeek.Thursday,
- Qt.DayOfWeek.Friday]:
- self.setWeekdayTextFormat(day, weekday_fmt)
-
- def paintCell(self, painter, rect, date):
- painter.save()
- painter.setRenderHint(QPainter.RenderHint.Antialiasing)
-
- # 绘制圆角背景
- cell_rect = rect.adjusted(2, 2, -2, -2)
- radius = 8
-
- # 设置背景颜色
- if date == QDate.currentDate():
- # 今天 - 浅绿色背景
- bg_color = QColor(230, 245, 230)
- elif date.dayOfWeek() > 5: # 周末
- bg_color = QColor(255, 240, 240)
- else: # 工作日
- bg_color = QColor(255, 255, 255)
-
- # 如果是选中日期,使用特殊样式
- if date == self.selectedDate():
- painter.setPen(QPen(QColor(100, 150, 255), 2)) # 蓝色边框
- painter.setBrush(QBrush(QColor(240, 245, 255))) # 浅蓝色背景
- painter.drawRoundedRect(cell_rect, radius, radius)
- else:
- # 普通日期背景
- painter.setPen(QPen(QColor(220, 220, 220), 1))
- painter.setBrush(QBrush(bg_color))
- painter.drawRoundedRect(cell_rect, radius, radius)
-
- # 设置文字颜色
- text_color = QColor(60, 60, 60) # 默认深灰色
- if date.dayOfWeek() > 5: # 周末
- text_color = QColor(220, 50, 50)
- if date == QDate.currentDate(): # 今天
- text_color = QColor(0, 100, 0)
- if date == self.selectedDate(): # 选中日期
- text_color = QColor(50, 100, 200) # 蓝色文字
-
- painter.setPen(QPen(text_color))
-
- # 绘制日期文本
- font = QFont("Arial", 9)
- if date == QDate.currentDate() or date == self.selectedDate():
- font.setBold(True)
- painter.setFont(font)
-
- painter.drawText(cell_rect, Qt.AlignmentFlag.AlignCenter, str(date.day()))
-
- painter.restore()
- class CalendarApp(QMainWindow):
- def __init__(self):
- super().__init__()
- self.setWindowTitle("精美日历")
- self.resize(450, 450)
-
- # 主窗口样式
- self.setStyleSheet("""
- QMainWindow {
- background: #f8f8f8;
- border-radius: 15px;
- }
- """)
-
- # 创建中央部件
- central_widget = QWidget()
- central_widget.setStyleSheet("background: transparent;")
- self.setCentralWidget(central_widget)
-
- # 布局
- layout = QVBoxLayout(central_widget)
- layout.setContentsMargins(20, 20, 20, 20)
- layout.setSpacing(15)
-
- # 标题
- title = QLabel("精美日历")
- title.setStyleSheet("""
- QLabel {
- font-size: 24px;
- font-weight: bold;
- color: #444444;
- padding: 5px;
- }
- """)
- title.setAlignment(Qt.AlignmentFlag.AlignCenter)
- layout.addWidget(title)
-
- # 日历部件
- self.calendar = RoundCellCalendar()
- layout.addWidget(self.calendar)
-
- # 底部按钮
- btn_today = QPushButton("返回今天")
- btn_today.setStyleSheet("""
- QPushButton {
- background: #4CAF50;
- color: white;
- border: none;
- padding: 8px;
- border-radius: 10px;
- font-size: 14px;
- min-width: 100px;
- }
- QPushButton:hover {
- background: #45a049;
- }
- """)
- btn_today.clicked.connect(self.go_to_today)
- layout.addWidget(btn_today, 0, Qt.AlignmentFlag.AlignHCenter)
- def go_to_today(self):
- self.calendar.setSelectedDate(QDate.currentDate())
- if __name__ == "__main__":
- app = QApplication(sys.argv)
-
- # 设置应用样式
- app.setStyle("Fusion")
- palette = app.palette()
- palette.setColor(QPalette.ColorRole.Window, QColor(248, 248, 248))
- app.setPalette(palette)
-
- window = CalendarApp()
- window.show()
- sys.exit(app.exec())
复制代码
分步骤教我上面的内容 |
|