198 lines
5.8 KiB
Python
198 lines
5.8 KiB
Python
import sys
|
|
from PyQt5.QtCore import Qt
|
|
from PyQt5.QtWidgets import (QApplication, QWidget, QVBoxLayout, QLabel,
|
|
QPushButton, QHBoxLayout, QSizePolicy, QFileDialog)
|
|
import os
|
|
|
|
|
|
class MainWindow(QWidget):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.setGeometry(100, 100, 1000, 800) # 设置窗口大小
|
|
|
|
# 使用布局而不是手动定位
|
|
layout = QVBoxLayout(self)
|
|
layout.setAlignment(Qt.AlignTop)
|
|
|
|
for i in range(3): # 减少数量便于查看
|
|
card = ProjectCard()
|
|
layout.addWidget(card)
|
|
|
|
|
|
class ProjectCard(QWidget):
|
|
def __init__(self, parent=None):
|
|
super().__init__(parent)
|
|
self.setObjectName("projectCard")
|
|
self.setFixedSize(280, 240)
|
|
self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
|
|
self.setAttribute(Qt.WA_StyledBackground) # 关键:启用样式背景
|
|
|
|
self.init_ui()
|
|
|
|
def init_ui(self):
|
|
"""创建网格布局"""
|
|
layout = QVBoxLayout(self)
|
|
layout.setContentsMargins(0, 0, 0, 0)
|
|
layout.setSpacing(0)
|
|
|
|
# 项目头部
|
|
self.create_project_header(layout)
|
|
|
|
# 项目图片
|
|
self.create_project_image(layout)
|
|
|
|
# 项目底部
|
|
self.create_project_footer(layout)
|
|
|
|
def create_project_header(self, layout):
|
|
"""创建项目头部"""
|
|
header_widget = QWidget()
|
|
header_widget.setObjectName("projectHeader")
|
|
header_layout = QHBoxLayout(header_widget)
|
|
header_layout.setContentsMargins(16, 16, 16, 8)
|
|
header_layout.setSpacing(8)
|
|
|
|
# 项目标题
|
|
title_label = QLabel("项目名称")
|
|
title_label.setObjectName("projectTitle")
|
|
title_label.setWordWrap(True)
|
|
header_layout.addWidget(title_label)
|
|
|
|
# 右侧操作区
|
|
actions_layout = QHBoxLayout()
|
|
actions_layout.setSpacing(4)
|
|
|
|
# 菜单按钮
|
|
self.menu_btn = QPushButton("⋯")
|
|
self.menu_btn.setObjectName("menuBtn")
|
|
self.menu_btn.setFixedSize(24, 24)
|
|
self.menu_btn.setToolTip("项目操作菜单")
|
|
actions_layout.addWidget(self.menu_btn)
|
|
|
|
header_layout.addLayout(actions_layout)
|
|
layout.addWidget(header_widget)
|
|
|
|
def create_project_image(self, layout):
|
|
"""创建项目图片区域"""
|
|
image_container = QWidget()
|
|
image_container.setObjectName("projectImageContainer")
|
|
|
|
container_layout = QVBoxLayout(image_container)
|
|
container_layout.setContentsMargins(16, 0, 16, 0) # 减少垂直边距
|
|
|
|
image_widget = QWidget()
|
|
image_widget.setObjectName("projectImage")
|
|
image_layout = QVBoxLayout(image_widget)
|
|
image_layout.setAlignment(Qt.AlignCenter)
|
|
image_layout.setContentsMargins(0, 0, 0, 0)
|
|
image_layout.setSpacing(8)
|
|
|
|
# 项目图标
|
|
icon_label = QLabel("图")
|
|
icon_label.setObjectName("projectIcon")
|
|
icon_label.setAlignment(Qt.AlignCenter)
|
|
image_layout.addWidget(icon_label)
|
|
|
|
container_layout.addWidget(image_widget)
|
|
layout.addWidget(image_container, 1) # 添加拉伸因子
|
|
|
|
def create_project_footer(self, layout):
|
|
"""创建项目底部"""
|
|
footer_widget = QWidget()
|
|
footer_widget.setObjectName("projectFooter")
|
|
footer_layout = QVBoxLayout(footer_widget)
|
|
footer_layout.setContentsMargins(16, 8, 16, 16)
|
|
|
|
# 项目日期
|
|
date_label = QLabel("2025年8月6日")
|
|
date_label.setObjectName("projectDate")
|
|
date_label.setAlignment(Qt.AlignCenter)
|
|
footer_layout.addWidget(date_label)
|
|
|
|
layout.addWidget(footer_widget)
|
|
|
|
|
|
class StyleSheet:
|
|
@staticmethod
|
|
def get_main_style():
|
|
return """
|
|
/* 基础卡片样式 - 修复继承问题 */
|
|
#projectCard {
|
|
background-color: #8b5cf6;
|
|
border: 1px solid #5a5a6a;
|
|
border-radius: 16px;
|
|
}
|
|
|
|
/* 简化悬停状态 */
|
|
#projectCard:hover {
|
|
background-color: #7c3aed;
|
|
border-color: #6a6a7a;
|
|
}
|
|
|
|
/* 头部样式 - 移除透明背景 */
|
|
#projectHeader {
|
|
background-color: #8b5cf6;
|
|
border-top-left-radius: 16px;
|
|
border-top-right-radius: 16px;
|
|
}
|
|
|
|
#projectTitle {
|
|
font-size: 14px;
|
|
font-weight: 600;
|
|
color: #ffffff;
|
|
background-color: transparent;
|
|
}
|
|
|
|
#menuBtn {
|
|
background-color: transparent;
|
|
border: none;
|
|
border-radius: 4px;
|
|
color: #ffffff;
|
|
font-size: 16px;
|
|
}
|
|
|
|
#menuBtn:hover {
|
|
background-color: rgba(255, 255, 255, 0.2);
|
|
}
|
|
|
|
/* 图片区域样式 - 调整尺寸和边距 */
|
|
#projectImageContainer {
|
|
background-color: transparent;
|
|
padding: 0px; /* 减少内边距 */
|
|
}
|
|
|
|
#projectImage {
|
|
background-color: #3a3a4a;
|
|
border-radius: 12px;
|
|
min-height: 100px; /* 减小最小高度 */
|
|
max-height: 120px;
|
|
}
|
|
|
|
#projectIcon {
|
|
font-size: 36px; /* 减小图标大小 */
|
|
color: #ffffff;
|
|
padding: 10px;
|
|
}
|
|
|
|
/* 底部样式 - 移除透明背景 */
|
|
#projectFooter {
|
|
background-color: #8b5cf6;
|
|
border-bottom-left-radius: 16px;
|
|
border-bottom-right-radius: 16px;
|
|
}
|
|
|
|
#projectDate {
|
|
font-size: 11px;
|
|
color: #e0e0ff; /* 更亮的颜色提高可读性 */
|
|
font-weight: 400;
|
|
background-color: transparent;
|
|
}
|
|
"""
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app = QApplication(sys.argv)
|
|
app.setStyleSheet(StyleSheet.get_main_style())
|
|
w = MainWindow()
|
|
w.show()
|
|
app.exec_() |