음력을 표시하기 위해 create_date_cell 메서드를 수정해보겠습니다. 음력 계산을 위해 lunardate 라이브러리를 사용할 수 있습니다. 먼저 해당 라이브러리를 설치해야 합니다:
pip install lunardate
이제 수정된 코드입니다:
from lunardate import LunarDate
def create_date_cell(self, day, dateForHoliday):
cell_widget = QWidget()
cell_widget.setObjectName("CellWidget")
layout = QVBoxLayout(cell_widget)
layout.setContentsMargins(1, 1, 1, 1)
layout.setSpacing(0)
date_layout = QHBoxLayout() # 날짜와 휴일 이름을 가로로 배치하기 위한 레이아웃
date_label = QLabel(str(day))
date_label.setAlignment(Qt.AlignLeft | Qt.AlignTop)
date_label.setFont(QFont("Arial", 11, QFont.Bold))
# 음력 레이블 추가
lunar_label = QLabel()
lunar_label.setAlignment(Qt.AlignLeft | Qt.AlignTop)
lunar_label.setFont(QFont("Arial", 8, QFont.Normal))
lunar_label.setStyleSheet("color: gray; background-color: transparent;")
holiday_label = QLabel()
holiday_label.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
holiday_label.setFont(QFont("Arial", 10, QFont.Normal))
date_label.setStyleSheet("""
QLabel {
color: black;
background-color: transparent;
padding: 0px 2px 0px 0px;
}
QLabel[weekday="5"] {
color: blue;
}
QLabel[weekday="6"], QLabel[isHoliday="true"] {
color: red;
}
""")
holiday_label.setStyleSheet("color: red; background-color: transparent;")
current_date = date(self.selected_date.year(), self.selected_date.month(), day)
date_label.setProperty("weekday", current_date.weekday())
# 음력 계산
lunar_date = LunarDate.from_solar_date(current_date.year, current_date.month, current_date.day)
lunar_text = lunar_date.get_lunar_date_string()
lunar_label.setText(lunar_text)
holiday = jpholiday.is_holiday(current_date)
holiday_name = jpholiday.is_holiday_name(current_date)
date_label.setProperty("isHoliday", str(bool(holiday)).lower())
if holiday:
holiday_label.setText(f"({holiday_name})")
# 레이아웃에 위젯 추가 (날짜, 음력, 휴일 순서로)
date_layout.addWidget(date_label)
date_layout.addWidget(lunar_label)
date_layout.addWidget(holiday_label)
date_layout.addStretch()
layout.addLayout(date_layout)
schedules = self.db.get_schedules_by_date(current_date.isoformat())
for schedule in schedules:
schedule_id, title, eventType = schedule
color = 'black' if eventType is None else eventType
title_label = ScheduleLabel(schedule_id, title, self.db, color, self.update_calendar)
layout.addWidget(title_label)
cell_widget.setStyleSheet("""
QWidget#CellWidget {
background-color: white;
border: none;
margin: 2px;
}
""")
layout.addStretch()
return cell_widget
이 코드는 다음과 같은 변경사항을 포함합니다:
lunardate 라이브러리를 임포트하여 음력 계산을 가능하게 합니다.
- 음력을 표시할 을 추가했습니다.