界面优化
This commit is contained in:
1
src/gui/components/__init__.py
Normal file
1
src/gui/components/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
# src.gui.components package
|
||||
78
src/gui/components/custom_widgets.py
Normal file
78
src/gui/components/custom_widgets.py
Normal file
@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
自定义组件 - 文件选择控件等公共组件
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from PyQt5.QtWidgets import (
|
||||
QWidget, QHBoxLayout, QLabel, QLineEdit, QPushButton, QFileDialog,
|
||||
)
|
||||
from PyQt5.QtCore import Qt
|
||||
|
||||
|
||||
class FileSelectWidget(QWidget):
|
||||
"""文件选择组件"""
|
||||
def __init__(self, label_text, file_filter="All Files (*.*)", mode="open", parent=None):
|
||||
"""
|
||||
初始化文件选择组件
|
||||
|
||||
Args:
|
||||
label_text: 标签文本
|
||||
file_filter: 文件过滤器
|
||||
mode: 选择模式 - "open"(打开文件) 或 "save"(保存文件)
|
||||
parent: 父控件
|
||||
"""
|
||||
super().__init__(parent)
|
||||
self.file_filter = file_filter
|
||||
self.mode = mode # "open" 或 "save"
|
||||
self.init_ui(label_text)
|
||||
|
||||
def init_ui(self, label_text):
|
||||
layout = QHBoxLayout()
|
||||
layout.setContentsMargins(0, 0, 0, 0)
|
||||
|
||||
self.label = QLabel(label_text)
|
||||
self.label.setMinimumWidth(120)
|
||||
self.line_edit = QLineEdit()
|
||||
placeholder = "请选择保存路径..." if self.mode == "save" else "请选择文件..."
|
||||
self.line_edit.setPlaceholderText(placeholder)
|
||||
self.browse_btn = QPushButton("浏览...")
|
||||
self.browse_btn.setMaximumWidth(80)
|
||||
self.browse_btn.clicked.connect(self.browse_file)
|
||||
|
||||
layout.addWidget(self.label)
|
||||
layout.addWidget(self.line_edit, 1)
|
||||
layout.addWidget(self.browse_btn)
|
||||
|
||||
self.setLayout(layout)
|
||||
|
||||
def browse_file(self):
|
||||
"""浏览文件"""
|
||||
current_text = self.line_edit.text().strip()
|
||||
initial_dir = ""
|
||||
|
||||
if current_text:
|
||||
dir_path = os.path.dirname(current_text)
|
||||
if dir_path and os.path.exists(dir_path):
|
||||
initial_dir = dir_path
|
||||
|
||||
if self.mode == "save":
|
||||
file_path, _ = QFileDialog.getSaveFileName(
|
||||
self, "保存文件", initial_dir, self.file_filter
|
||||
)
|
||||
else:
|
||||
file_path, _ = QFileDialog.getOpenFileName(
|
||||
self, "选择文件", initial_dir, self.file_filter
|
||||
)
|
||||
if file_path:
|
||||
self.line_edit.setText(file_path)
|
||||
|
||||
def get_path(self):
|
||||
"""获取路径"""
|
||||
return self.line_edit.text()
|
||||
|
||||
def set_path(self, path):
|
||||
"""设置路径"""
|
||||
self.line_edit.setText(str(path))
|
||||
Reference in New Issue
Block a user