155 lines
4.5 KiB
Python
155 lines
4.5 KiB
Python
"""
|
|
sudo 密码输入对话框
|
|
"""
|
|
from PyQt5.QtWidgets import (
|
|
QDialog, QVBoxLayout, QHBoxLayout, QLabel,
|
|
QLineEdit, QPushButton, QMessageBox
|
|
)
|
|
from PyQt5.QtCore import Qt
|
|
from PyQt5.QtGui import QFont, QIcon
|
|
|
|
|
|
class SudoDialog(QDialog):
|
|
"""sudo 密码输入对话框"""
|
|
|
|
def __init__(self, parent=None):
|
|
super().__init__(parent)
|
|
self.password = None
|
|
self.init_ui()
|
|
|
|
def init_ui(self):
|
|
"""初始化界面"""
|
|
self.setWindowTitle("需要 sudo 权限")
|
|
self.setFixedSize(400, 200)
|
|
self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint)
|
|
|
|
# 主布局
|
|
layout = QVBoxLayout(self)
|
|
layout.setSpacing(15)
|
|
layout.setContentsMargins(20, 20, 20, 20)
|
|
|
|
# 图标和提示
|
|
icon_label = QLabel("🔒")
|
|
icon_label.setAlignment(Qt.AlignCenter)
|
|
font = QFont()
|
|
font.setPointSize(36)
|
|
icon_label.setFont(font)
|
|
layout.addWidget(icon_label)
|
|
|
|
# 提示文本
|
|
hint_label = QLabel(
|
|
"本工具需要 sudo 权限来访问磁盘分区和删除文件。\n"
|
|
"请输入 sudo 密码:"
|
|
)
|
|
hint_label.setAlignment(Qt.AlignCenter)
|
|
hint_label.setWordWrap(True)
|
|
layout.addWidget(hint_label)
|
|
|
|
# 密码输入框
|
|
self.password_input = QLineEdit()
|
|
self.password_input.setEchoMode(QLineEdit.Password)
|
|
self.password_input.setPlaceholderText("请输入 sudo 密码")
|
|
self.password_input.returnPressed.connect(self.on_ok_clicked)
|
|
layout.addWidget(self.password_input)
|
|
|
|
# 按钮
|
|
button_layout = QHBoxLayout()
|
|
button_layout.addStretch()
|
|
|
|
self.cancel_button = QPushButton("取消")
|
|
self.cancel_button.clicked.connect(self.on_cancel_clicked)
|
|
button_layout.addWidget(self.cancel_button)
|
|
|
|
self.ok_button = QPushButton("确定")
|
|
self.ok_button.setDefault(True)
|
|
self.ok_button.clicked.connect(self.on_ok_clicked)
|
|
button_layout.addWidget(self.ok_button)
|
|
|
|
layout.addLayout(button_layout)
|
|
|
|
# 设置焦点
|
|
self.password_input.setFocus()
|
|
|
|
def on_ok_clicked(self):
|
|
"""确定按钮点击"""
|
|
password = self.password_input.text()
|
|
|
|
if not password:
|
|
QMessageBox.warning(
|
|
self,
|
|
"警告",
|
|
"请输入密码!"
|
|
)
|
|
return
|
|
|
|
self.password = password
|
|
self.accept()
|
|
|
|
def on_cancel_clicked(self):
|
|
"""取消按钮点击"""
|
|
self.password = None
|
|
self.reject()
|
|
|
|
def get_password(self) -> str:
|
|
"""获取输入的密码"""
|
|
return self.password
|
|
|
|
|
|
class SudoVerifyDialog(QDialog):
|
|
"""sudo 密码验证对话框(显示验证进度)"""
|
|
|
|
def __init__(self, parent=None):
|
|
super().__init__(parent)
|
|
self.password = None
|
|
self.init_ui()
|
|
|
|
def init_ui(self):
|
|
"""初始化界面"""
|
|
self.setWindowTitle("验证 sudo 密码")
|
|
self.setFixedSize(300, 150)
|
|
self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint)
|
|
|
|
# 主布局
|
|
layout = QVBoxLayout(self)
|
|
layout.setSpacing(15)
|
|
layout.setContentsMargins(20, 20, 20, 20)
|
|
|
|
# 提示文本
|
|
self.status_label = QLabel("正在验证 sudo 密码...")
|
|
self.status_label.setAlignment(Qt.AlignCenter)
|
|
layout.addWidget(self.status_label)
|
|
|
|
# 进度指示
|
|
self.progress_label = QLabel("⏳")
|
|
self.progress_label.setAlignment(Qt.AlignCenter)
|
|
font = QFont()
|
|
font.setPointSize(24)
|
|
self.progress_label.setFont(font)
|
|
layout.addWidget(self.progress_label)
|
|
|
|
# 取消按钮
|
|
button_layout = QHBoxLayout()
|
|
button_layout.addStretch()
|
|
|
|
self.cancel_button = QPushButton("取消")
|
|
self.cancel_button.clicked.connect(self.reject)
|
|
button_layout.addWidget(self.cancel_button)
|
|
|
|
layout.addLayout(button_layout)
|
|
|
|
def set_status(self, status: str):
|
|
"""设置状态文本"""
|
|
self.status_label.setText(status)
|
|
|
|
def set_success(self):
|
|
"""设置验证成功状态"""
|
|
self.progress_label.setText("✅")
|
|
self.status_label.setText("验证成功!")
|
|
self.cancel_button.setText("关闭")
|
|
|
|
def set_failure(self, message: str):
|
|
"""设置验证失败状态"""
|
|
self.progress_label.setText("❌")
|
|
self.status_label.setText(f"验证失败:{message}")
|
|
self.cancel_button.setText("关闭")
|