Skip to content

Commit

Permalink
release-v1.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Yang-ZhiHang committed Nov 17, 2024
1 parent bccfa30 commit f2a6238
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 35 deletions.
11 changes: 11 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
from utils.config_handler import ConfigHandler
from ui.base_window import BaseWindow
from ui.components.progress_handler import ProgressHandler
from utils.style_handler import StyleManager
import ctypes

ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("myappid")


class XDUScript(BaseWindow):
def __init__(self):
super().__init__()
Expand All @@ -20,6 +23,9 @@ def __init__(self):
# 连接开始按钮的点击事件
self.start_button.clicked.connect(self.config_handler.get_config)

# 样式处理器属性
self.style_handler = None

# 初始化时打开网页
open_website(
"https://ehall.xidian.edu.cn/jwapp/sys/wspjyyapp/*default/index.do",
Expand All @@ -30,5 +36,10 @@ def __init__(self):
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = XDUScript()

# 加载样式
ex.style_handler = StyleManager(ex, app)
ex.style_handler.apply_styles()

ex.show()
sys.exit(app.exec_())
4 changes: 2 additions & 2 deletions main.spec
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ a = Analysis(
'D:/dev/XDU_WallNut/utils/__init__.py',
'D:/dev/XDU_WallNut/utils/browser_utils.py',
'D:/dev/XDU_WallNut/utils/config_handler.py',
'D:/dev/XDU_WallNut/utils/style_loader.py',
'D:/dev/XDU_WallNut/utils/style_handler.py',
], # 此项目中所有的 python 脚本
pathex=['D:\\dev\\XDU_WallNut'], # 项目绝对路径
binaries=[],
datas=[
('styles/style.qss', 'styles'),
('styles/base.qss', 'styles'),
('favicon.ico', '.')
],
hiddenimports=[],
Expand Down
3 changes: 0 additions & 3 deletions styles/style.qss → styles/base.qss
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ QWidget {

QLabel {
color: #e0e0e0;
font-size: 14px;
}

QLineEdit {
Expand All @@ -29,7 +28,6 @@ QPushButton {
border: none;
padding: 8px 16px;
border-radius: 4px;
font-size: 13px;
}

QPushButton:hover {
Expand All @@ -52,7 +50,6 @@ QTextEdit {
border-radius: 4px;
padding: 8px;
font-family: 'Consolas', 'Microsoft YaHei Mono';
font-size: 13px;
selection-background-color: #3d7aab;
}

Expand Down
10 changes: 1 addition & 9 deletions ui/base_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import os
sys.path.append('utils')

from utils.style_loader import load_stylesheet
from ui.components.input_section import InputSection

class BaseWindow(QWidget):
Expand All @@ -20,7 +19,7 @@ def __init__(self):
def initUI(self):

# 设置窗口大小
self.setGeometry(100, 100, 400, 400)
self.setGeometry(100, 100, 600, 400)

# 设置窗口标题
self.setWindowTitle("XDU一键评教")
Expand All @@ -33,13 +32,6 @@ def initUI(self):
ret_path = os.path.join(base_path, "favicon.ico")
self.setWindowIcon(QIcon(ret_path))



# 加载样式表
style = load_stylesheet()
if style:
self.setStyleSheet(style)

# 初始化组件
self.input_section = InputSection(self)
self.input_section.init_layout_auto_select()
Expand Down
84 changes: 84 additions & 0 deletions utils/style_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import os
import sys

from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QSize


class StyleManager:
def __init__(self, window, app):
self.window = window
self.app = app
# 基准字体大小(类似于 rem 的基准值)
self.base_font_size = 16
self.update_scale_factor()

def update_scale_factor(self):
# 获取屏幕 DPI 缩放比例
screen = self.app.primaryScreen()
self.dpi_scale = screen.logicalDotsPerInch() / 96.0

# 获取屏幕高度(类似于 vh)
self.screen_height = screen.size().height()

# 计算实际的基准大小
self.scaled_base_size = self.base_font_size * self.dpi_scale

def rem(self, value):
"""将 rem 值转换为实际像素值"""
return int(value * self.scaled_base_size)

def vh(self, value):
"""将 vh 值转换为实际像素值"""
return int(self.screen_height * value / 100)

def init_responsive_font_styles(self):
# 生成响应式样式表
style_sheet = f"""
QLabel {{
font-size: {self.rem(1)}px; /* 1rem */
min-height: {self.vh(3)}px; /* 5vh */
}}
QPushButton {{
font-size: {self.rem(0.875)}px; /* 0.875rem */
padding: {self.rem(0.5)}px {self.rem(1)}px;
}}
QTextEdit {{
font-size: {self.rem(0.875)}px;
min-height: {self.vh(10)}px; /* 30vh */
}}
QRadioButton {{
font-size: {self.rem(0.875)}px;
min-height: {self.vh(3)}px; /* 5vh */
}}
"""
return style_sheet

def init_base_stylesheet(self):
try:
# 获取应用程序的基础路径
if getattr(sys, "frozen", False):
# 如果是打包后的可执行文件
base_path = sys._MEIPASS
else:
# 如果是开发环境
base_path = os.path.abspath(".")

# 构建样式文件的完整路径
style_file = os.path.join(base_path, "styles", "base.qss")

with open(style_file, "r", encoding="utf-8") as f:
return f.read()
except Exception as e:
self.window.console_output.append(f"样式表加载失败:{str(e)}")
return ""

def apply_styles(self):
"""合并并应用所有样式"""
base_styles = self.init_base_stylesheet()
responsive_styles = self.init_responsive_font_styles()
combined_styles = base_styles + "\n" + responsive_styles
self.window.setStyleSheet(combined_styles)
21 changes: 0 additions & 21 deletions utils/style_loader.py

This file was deleted.

0 comments on commit f2a6238

Please sign in to comment.