能读取到信息,但是只显示当天的,切换另一天打卡界面不显示,请修改
class HomeInterface(QWidget):
def __init__(self, employee_id, name, department):
super().__init__()
self.setObjectName('homeInterface')
self.employee_id = employee_id
self.name = name
self.department = department
self.initUI()
def initUI(self):
# 主布局 - 网格布局
main_layout = QGridLayout(self)
main_layout.setSpacing(20)
main_layout.setContentsMargins(40, 30, 40, 30)
# 1. 员工问候语区域 (顶部)
greeting_card = self.create_greeting_card()
main_layout.addWidget(greeting_card, 0, 0, 1, 4) # 占据第一行4列
# 2. 统计看板 (4个卡片)
stats_cards = self.create_stats_cards()
for i, card in enumerate(stats_cards):
main_layout.addWidget(card, 1, i) # 第二行,每列一个卡片
# 3. 日历和打卡区域 (左右分栏)
today = QDate.currentDate()
calendar_card = self.create_calendar_card(today.year(),today.month())
attendance_card = self.create_attendance_card(today)
# 创建左右分栏容器
bottom_container = QWidget()
bottom_layout = QHBoxLayout(bottom_container)
bottom_layout.setSpacing(20)
bottom_layout.addWidget(calendar_card, 2)
bottom_layout.addWidget(attendance_card, 1) # 打卡区域占2/3宽度
main_layout.addWidget(bottom_container, 2, 0, 1, 4) # 第三行,占据4列
self.setLayout(main_layout)
def create_greeting_card(self) -> CardWidget:
"""创建问候语卡片"""
card = CardWidget(self)
card.setMinimumHeight(100)
layout = QHBoxLayout(card)
layout.setContentsMargins(30, 20, 30, 20)
# 问候语文本
greeting_text = BodyLabel(f"早上好,{self.name}!祝您有愉快的一天!")
greeting_text.setFont(QFont("Microsoft YaHei", 18, QFont.Bold))
# 打卡按钮
checkin_btn = PrimaryPushButton(text="返回登录界面")
checkin_btn.clicked.connect(self.return_to_login)
checkin_btn.setFixedSize(150, 40)
layout.addWidget(greeting_text, 1)
layout.addWidget(checkin_btn)
return card
def return_to_login(self):
from ui_login import LoginWindow # 新增导入
self.login_window = LoginWindow()
self.login_window.show()
self.close()
def create_stats_cards(self) -> List[widgets.CardWidget]:
"""创建4个统计看板卡片"""
stats_data = [
{"title": "本月出勤", "value": f"{22}天"},
{"title": "考勤异常", "value": f"{5}次"},
{"title": "实际工时", "value": f"{3}小时"},
{"title": "期望工时", "value": f"{75}小时"}
]
cards = []
for data in stats_data:
card = CardWidget(self)
card.setFixedHeight(120)
layout = QVBoxLayout(card)
layout.setContentsMargins(20, 15, 20, 15)
# 标题
title = CaptionLabel(data["title"])
title.setStyleSheet("color: #666666;")
# 主数值
value = BodyLabel(data["value"])
value.setFont(QFont("Microsoft YaHei", 20, QFont.Bold))
value.setStyleSheet("color: #28afe9; margin: 5px 0;")
layout.addWidget(title)
layout.addWidget(value)
cards.append(card)
return cards
def create_calendar_card(self,year,month) -> CardWidget:
"""创建日历卡片"""
card = CardWidget(self)
card.setMinimumHeight(400)
layout = QVBoxLayout(card)
layout.setContentsMargins(20, 20, 20, 20)
# 标题
title = BodyLabel("工作日历")
title.setFont(QFont("Microsoft YaHei", 12, QFont.Bold))
calendar = HeatmapCalendar()
calendar.setGridVisible(True)
calendar.setMinimumDate(QDate.currentDate().addMonths(-3))
calendar.setMaximumDate(QDate.currentDate().addMonths(1))
calendar.setCurrentPage(QDate.currentDate().year(), QDate.currentDate().month())
calendar.clicked[QDate].connect(self.create_attendance_card)
calendar.setStyleSheet('''
QCalendarWidget QWidget#qt_calendar_navigationbar {
background-color: #E8E8E8;
color: white;
font-size: 20px;
font-weight: bold;
}
QToolButton {
color: balck;
font-size: 18px;
border: none;
}
QToolButton:hover {
background-color: #81C784;
}
''')
# 标记今日日期
today_format = calendar.weekdayTextFormat(Qt.Saturday)
today_format.setBackground(Qt.yellow)
calendar.setDateTextFormat(QDate.currentDate(), today_format)
month_str = f"{year}-{month:02d}"
conn = sqlite3.connect('badge_records.db')
cursor = conn.cursor()
cursor.execute('''
SELECT date(datetime(timestamp, 'unixepoch', '+8 hours')),
SUM(actual_work_hours),
SUM(expected_work_hours)
FROM records
WHERE badge_id=? AND strftime('%Y-%m', datetime(timestamp, 'unixepoch', '+8 hours'))=?
GROUP BY date(datetime(timestamp, 'unixepoch', '+8 hours'))
ORDER BY date(datetime(timestamp, 'unixepoch', '+8 hours'))
''', (self.employee_id, month_str))
records = cursor.fetchall()
conn.close()
# 构建热力图数据
heatmap_data = {}
for row, record in enumerate(records):
date, actual, expected = record
# 存入热力图数据
heatmap_data[date] = actual
calendar.set_heat_data(heatmap_data)
layout.addWidget(title)
layout.addWidget(calendar)
return card
def create_attendance_card(self,date:QDate) -> CardWidget:
"""创建打卡记录卡片"""
card = CardWidget(self)
card.setMinimumHeight(400)
date_str = date.toString("yyyy-MM-dd")
print(f"查询日期{date_str}")
layout = QVBoxLayout(card)
layout.setContentsMargins(20, 20, 20, 20)
# 标题栏
title = BodyLabel("今日打卡记录")
title.setFont(QFont("Microsoft YaHei", 12, QFont.Bold))
# 打卡记录
records_container = QWidget()
records_layout = QVBoxLayout(records_container)
records_layout.setSpacing(10)
# 添加记录
conn = sqlite3.connect('badge_records.db')
cursor = conn.cursor()
# 获取当日打卡记录
cursor.execute('''
SELECT strftime('%H:%M', datetime(timestamp, 'unixepoch', '+8 hours')), check_in_type, status, actual_work_hours
FROM records
WHERE badge_id=? AND date(datetime(timestamp, 'unixepoch', '+8 hours'))=?
ORDER BY datetime(timestamp, 'unixepoch', '+8 hours')
''', (self.employee_id, date_str))
records = cursor.fetchall()
conn.close()
print(f"查询结果: {records}")
# for row, record in enumerate(records):
# time, check_in_type, status, hours=record
# # 状态(翻译为中文)
# status_map = {
# "normal": "正常",
# "late": "迟到",
# "leave_early": "早退",
# "no_morning_check": "未上班打卡"
# }
# status_text = status_map.get(record[2], record[2])
# # self.daily_table.setItem(row, 2, QTableWidgetItem(status_text))
# # 设置不同状态的颜色
# color = QColor(0, 0, 0) # 默认黑色
# if status_text == '迟到' or status_text == '早退':
# color = QColor(255, 165, 0) # 橙色
# elif status_text == '缺勤':
# color = QColor(255, 0, 0) # 红色
# record_widget = self.create_record_item(time, check_in_type, status_text,color)
# records_layout.addWidget(record_widget)
# layout.addWidget(title)
# layout.addWidget(records_container)
return self.create_attendance_card_content(date_str, records)
def create_attendance_card_content(self, date_str, records):
"""创建打卡记录卡片内容"""
card = CardWidget(self)
layout = QVBoxLayout(card)
# 标题
title = BodyLabel(f"{date_str} 打卡记录")
layout.addWidget(title)
# 无记录时的处理
if not records:
no_data = BodyLabel("当日无打卡记录")
layout.addWidget(no_data)
return card
# 有记录时的处理
records_container = QWidget()
records_layout = QVBoxLayout(records_container)
for record in records:
time, check_in_type, status, hours = record
record_widget = self.create_record_item(time, check_in_type, status)
records_layout.addWidget(record_widget)
layout.addWidget(records_container)
return card
def create_record_item(self, time: str, action: str, location: str) -> QWidget:
"""创建单个打卡记录项"""
item = QWidget()
layout = QHBoxLayout(item)
layout.setContentsMargins(10, 8, 10, 8)
# 时间标签
time_label = BodyLabel(time)
time_label.setFixedWidth(60)
time_label.setStyleSheet("font-weight: bold;")
# 动作标签
action_label = BodyLabel(action)
action_label.setStyleSheet("color: #333333;")
# 位置标签
location_label = CaptionLabel(location)
location_label.setStyleSheet("color: #666666;")
layout.addWidget(time_label)
layout.addWidget(action_label)
layout.addStretch(1)
layout.addWidget(location_label)
# 添加样式
item.setStyleSheet("""
QWidget {
background-color: #F5F5F5;
border-radius: 6px;
}
""")
return item
最新发布