|
|
@@ -0,0 +1,119 @@
|
|
|
+# -*- coding: utf-8 -*-
|
|
|
+from PyQt5.QtWidgets import (
|
|
|
+ QDialog, QVBoxLayout, QGridLayout, QPushButton,
|
|
|
+ QLineEdit, QMessageBox, QWidget
|
|
|
+)
|
|
|
+from PyQt5.QtCore import Qt
|
|
|
+from PyQt5.QtGui import QFont
|
|
|
+
|
|
|
+class NumericKeypad(QDialog):
|
|
|
+ """
|
|
|
+ 数字密码键盘控件
|
|
|
+
|
|
|
+ 用于验证操作权限,默认密码为 199510
|
|
|
+ """
|
|
|
+ def __init__(self, parent=None):
|
|
|
+ super().__init__(parent)
|
|
|
+ self.setWindowTitle("请输入密码")
|
|
|
+ self.setFixedSize(320, 420)
|
|
|
+ self.setWindowFlags(Qt.Dialog | Qt.CustomizeWindowHint | Qt.WindowTitleHint | Qt.WindowCloseButtonHint)
|
|
|
+
|
|
|
+ # 验证密码
|
|
|
+ self.target_password = "199510"
|
|
|
+
|
|
|
+ self._init_ui()
|
|
|
+ self._setup_styles()
|
|
|
+
|
|
|
+ def _init_ui(self):
|
|
|
+ # 主布局
|
|
|
+ layout = QVBoxLayout()
|
|
|
+ layout.setSpacing(15)
|
|
|
+ layout.setContentsMargins(20, 20, 20, 20)
|
|
|
+ self.setLayout(layout)
|
|
|
+
|
|
|
+ # 显示框
|
|
|
+ self.display = QLineEdit()
|
|
|
+ self.display.setEchoMode(QLineEdit.Password)
|
|
|
+ self.display.setReadOnly(True)
|
|
|
+ self.display.setAlignment(Qt.AlignCenter)
|
|
|
+ self.display.setMinimumHeight(50)
|
|
|
+ self.display.setPlaceholderText("请输入6位数字密码")
|
|
|
+ layout.addWidget(self.display)
|
|
|
+
|
|
|
+ # 键盘区域
|
|
|
+ grid_layout = QGridLayout()
|
|
|
+ grid_layout.setSpacing(10)
|
|
|
+
|
|
|
+ # 按钮配置
|
|
|
+ buttons = [
|
|
|
+ ('1', 0, 0), ('2', 0, 1), ('3', 0, 2),
|
|
|
+ ('4', 1, 0), ('5', 1, 1), ('6', 1, 2),
|
|
|
+ ('7', 2, 0), ('8', 2, 1), ('9', 2, 2),
|
|
|
+ ('删除', 3, 0), ('0', 3, 1), ('确认', 3, 2),
|
|
|
+ ]
|
|
|
+
|
|
|
+ for text, row, col in buttons:
|
|
|
+ btn = QPushButton(text)
|
|
|
+ btn.setFixedSize(80, 60)
|
|
|
+ btn.clicked.connect(lambda checked, t=text: self._on_btn_clicked(t))
|
|
|
+ grid_layout.addWidget(btn, row, col)
|
|
|
+
|
|
|
+ layout.addLayout(grid_layout)
|
|
|
+
|
|
|
+ def _setup_styles(self):
|
|
|
+ self.setStyleSheet("""
|
|
|
+ QDialog {
|
|
|
+ background-color: #f0f0f0;
|
|
|
+ }
|
|
|
+ QLineEdit {
|
|
|
+ background-color: white;
|
|
|
+ border: 2px solid #ddd;
|
|
|
+ border-radius: 8px;
|
|
|
+ font-size: 24px;
|
|
|
+ color: #333;
|
|
|
+ padding: 5px;
|
|
|
+ }
|
|
|
+ QPushButton {
|
|
|
+ background-color: white;
|
|
|
+ border: 1px solid #ddd;
|
|
|
+ border-radius: 8px;
|
|
|
+ font-size: 20px;
|
|
|
+ color: #333;
|
|
|
+ }
|
|
|
+ QPushButton:hover {
|
|
|
+ background-color: #e6e6e6;
|
|
|
+ }
|
|
|
+ QPushButton:pressed {
|
|
|
+ background-color: #d0d0d0;
|
|
|
+ }
|
|
|
+ """)
|
|
|
+
|
|
|
+ def _on_btn_clicked(self, text):
|
|
|
+ current_text = self.display.text()
|
|
|
+
|
|
|
+ if text == '删除':
|
|
|
+ self.display.setText(current_text[:-1])
|
|
|
+ elif text == '确认':
|
|
|
+ self._verify_password()
|
|
|
+ else:
|
|
|
+ if len(current_text) < 6: # 限制长度
|
|
|
+ self.display.setText(current_text + text)
|
|
|
+
|
|
|
+ def _verify_password(self):
|
|
|
+ input_pwd = self.display.text()
|
|
|
+ if input_pwd == self.target_password:
|
|
|
+ self.accept() # 验证通过,关闭对话框并返回 QDialog.Accepted
|
|
|
+ else:
|
|
|
+ QMessageBox.warning(self, "验证失败", "密码错误,请重试!")
|
|
|
+ self.display.clear()
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def verify(parent=None):
|
|
|
+ """
|
|
|
+ 静态方法,方便调用
|
|
|
+ 返回: bool (验证是否通过)
|
|
|
+ """
|
|
|
+ dialog = NumericKeypad(parent)
|
|
|
+ result = dialog.exec_()
|
|
|
+ return result == QDialog.Accepted
|
|
|
+
|