PyQt: 登录注册窗口 / 窗口编辑状态 / 密码显示模式切换 / 验证器 / 输入掩码 / 状态栏 / What's this / 工具提示PyQt: 登录注册窗口 / 窗口编辑状态 / 密码显示模式切换 / 验证器 / 输入掩码 / 状态栏 / What's this / 工具提示PyQt: 登录注册窗口 / 窗口编辑状态 / 密码显示模式切换 / 验证器 / 输入掩码 / 状态栏 / What's this / 工具提示PyQt: 登录注册窗口 / 窗口编辑状态 / 密码显示模式切换 / 验证器 / 输入掩码 / 状态栏 / What's this / 工具提示
  • 首页
  • 博客
  • 书签
  • 文件
  • 分析
  • 登录

PyQt: 登录注册窗口 / 窗口编辑状态 / 密码显示模式切换 / 验证器 / 输入掩码 / 状态栏 / What's this / 工具提示

发表 admin at 2022年3月31日
类别
  • Practice
标签
import sys
from PyQt5.Qt import *


class AccountUtilities(object):
    # 定义账户信息设置与检查类
    user_name = None
    user_password = None

    @staticmethod
    def set_account(user_name, user_password):
        AccountUtilities.user_name = user_name
        AccountUtilities.user_password = user_password

    @staticmethod
    def check_account(user_name, user_password):
        # 用户名、密码均正确返回 0 ,用户名错误返回 1 ,密码错误返回 2
        if user_name != AccountUtilities.user_name:
            return 1
        if user_password != AccountUtilities.user_password:
            return 2
        return 0


class AgeValidator(QValidator):
    def validate(self, a0: str, a1: int) -> ():
        try:
            if len(a0) == 0:
                return (QValidator.Intermediate, a0, a1)
            elif 18 <= int(a0) <= 180:
                return (QValidator.Acceptable, a0, a1)
            elif 0 < int(a0) < 18:
                return (QValidator.Intermediate, a0, a1)
            else:
                return (QValidator.Invalid, a0, a1)
        except:
            return (QValidator.Invalid, a0, a1)

    def fixup(self, a0: str) -> str:
        return "18"


class RegisterWindow(QMainWindow):
    # 注册窗口
    def __init__(self):
        super().__init__()
        self.setup_ui()
        self.user_action()

    def resizeEvent(self, a0) -> None:
        # 重写resizeEvent信号处理方法,窗口大小变化时,控件位置跟随变化
        window_width = a0.size().width()
        window_height = a0.size().height()

        self.label_title.move(int(round((window_width - self.label_title.width()) / 2, 0)),
                              int(round(window_height / 10, 0)))
        self.line_edit_user_name.move(int(round((window_width - self.line_edit_user_name.width()) / 2, 0)),
                                      int(round(window_height / 10 * 2.5, 0)))
        self.line_edit_user_password.move(int(round((window_width - self.line_edit_user_password.width()) / 2, 0)),
                                          int(round(window_height / 10 * 4, 0)))
        self.line_edit_user_age.move(int(round((window_width - self.line_edit_user_password.width()) / 2, 0)),
                                     int(round(window_height / 10 * 5.5, 0)))
        self.line_edit_user_tel.move(int(round((window_width - self.line_edit_user_password.width()) / 2, 0)),
                                     int(round(window_height / 10 * 7, 0)))
        self.register_button.move(int(round((window_width - self.register_button.width()) / 2, 0)),
                                  int(round(window_height / 10 * 8.5, 0)))

    def setup_ui(self):
        self.setWindowTitle("注册")
        self.resize(300, 400)
        self.setMinimumSize(260, 260)

        self.label_info()
        self.line_edit()
        self.push_button()

    def label_info(self):
        self.label_title = QLabel(self)
        self.label_title.setText("请注册!")
        self.label_title.resize(80, 30)

    def line_edit(self):
        self.line_edit_user_name = QLineEdit(self)
        # 设置占位字符
        self.line_edit_user_name.setPlaceholderText("用户名")
        self.line_edit_user_name.resize(150, 30)
        self.line_edit_user_name.setMaxLength(8)
        # 开启清空按钮
        self.line_edit_user_name.setClearButtonEnabled(True)

        self.line_edit_user_password = QLineEdit(self)
        self.line_edit_user_password.setPlaceholderText("密码")
        self.line_edit_user_password.resize(150, 30)
        self.line_edit_user_password.setMaxLength(8)
        self.line_edit_user_password.setClearButtonEnabled(True)

        self.line_edit_user_age = QLineEdit(self)
        self.line_edit_user_age.setPlaceholderText("年龄18-180")

        # 设置验证器
        line_edit_user_age_validator = AgeValidator(self.line_edit_user_age)
        self.line_edit_user_age.setValidator(line_edit_user_age_validator)

        self.line_edit_user_age.resize(150, 30)
        self.line_edit_user_age.setMaxLength(8)
        self.line_edit_user_age.setClearButtonEnabled(True)

        class LineEditUserTel(QLineEdit):
            # 重写焦点事件处理方法,设置掩码
            def focusInEvent(self, a0) -> None:
                self.setInputMask("9999-99999999")
                return super(LineEditUserTel, self).focusInEvent(a0)

            def focusOutEvent(self, a0) -> None:
                if self.text() == "-":
                    self.setInputMask("")
                return super(LineEditUserTel, self).focusOutEvent(a0)

        self.line_edit_user_tel = LineEditUserTel(self)
        self.line_edit_user_tel.setPlaceholderText("电话号码")
        self.line_edit_user_tel.resize(150, 30)
        self.line_edit_user_tel.setMaxLength(13)
        self.line_edit_user_tel.setClearButtonEnabled(True)

    def push_button(self):
        self.register_button = QPushButton(self)
        self.register_button.setText("注册")
        self.register_button.setToolTip("注册")

    def user_action(self):
        # 定义密码框显示模式动作
        line_edit_user_password_echo_mode = QAction(self.line_edit_user_password)

        def line_edit_user_password_echo_mode_switch():
            # 密码显示模式切换
            if self.line_edit_user_password.echoMode() == QLineEdit.Password:
                line_edit_user_password_echo_mode.setIcon(QIcon("echo_mode_text.png"))
                self.line_edit_user_password.setEchoMode(QLineEdit.Normal)
            else:
                line_edit_user_password_echo_mode.setIcon(QIcon("echo_mode_password.png"))
                self.line_edit_user_password.setEchoMode(QLineEdit.Password)

        # 首次运行即设置密码框显示模式
        line_edit_user_password_echo_mode_switch()
        # 添加密码框显示模式自定义动作
        self.line_edit_user_password.addAction(line_edit_user_password_echo_mode, QLineEdit.TrailingPosition)
        # 连接密码框显示模式信号与槽
        line_edit_user_password_echo_mode.triggered.connect(line_edit_user_password_echo_mode_switch)


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        # 设置账户信息
        AccountUtilities.set_account("abc", "123")
        # self.line_edit_user_account_state = {"line_edit_user_name_state": False, "line_edit_user_password_state": False}
        self.setup_ui()
        self.user_action()

    def resizeEvent(self, a0) -> None:
        # 重写resizeEvent信号处理方法,窗口大小变化时,控件位置跟随变化
        window_width = a0.size().width()
        window_height = a0.size().height()

        self.label_title.move(int(round((window_width - self.label_title.width()) / 2, 0)),
                              int(round(window_height / 10, 0)))

        self.line_edit_user_name.move(int(round((window_width - self.line_edit_user_name.width()) / 2, 0)),
                                      int(round(window_height / 10 * 4, 0)))
        self.label_user_name_tip.move(self.line_edit_user_name.x(),
                                      int(round(window_height / 10 * 3.4, 0)))

        self.line_edit_user_password.move(int(round((window_width - self.line_edit_user_password.width()) / 2, 0)),
                                          int(round(window_height / 10 * 6, 0)))
        self.label_user_password_tip.move(self.line_edit_user_password.x(),
                                          int(round(window_height / 10 * 5.4, 0)))

        self.login_submit_button.move(
            int(round((window_width - self.login_submit_button.width() - self.register_button.width()) / 2, 0)),
            int(round(window_height / 10 * 8, 0)))
        self.register_button.move(self.login_submit_button.x() + self.login_submit_button.width(),
                                  self.login_submit_button.y())

    def setup_ui(self):
        # 开启状态栏
        self.statusBar()
        # 开启What's this提示
        self.setWindowFlags(Qt.WindowContextHelpButtonHint | Qt.WindowCloseButtonHint)
        self.user_account_list = ["abc", "acd", "bcd", "bde"]
        # [*]中的*在setWindowModified(True)时显示
        self.setWindowTitle("登录[*]")
        self.resize(350, 350)
        self.setMinimumSize(260, 260)

        self.label_info()
        self.line_edit()
        self.push_button()

    def label_info(self):
        self.label_title = QLabel(self)
        self.label_title.setText("请登录!")
        self.label_title.resize(80, 30)
        # 状态栏提示
        self.label_title.setStatusTip(f"用户名:{AccountUtilities.user_name} 密码:{AccountUtilities.user_password}")

        self.label_user_name_tip = QLabel(self)
        self.label_user_name_tip.setText("用户名错误!")
        self.label_user_name_tip.adjustSize()
        self.label_user_name_tip.setVisible(False)

        self.label_user_password_tip = QLabel(self)
        self.label_user_password_tip.setText("密码错误!")
        self.label_user_password_tip.adjustSize()
        self.label_user_password_tip.setVisible(False)

    def line_edit(self):
        self.line_edit_user_name = QLineEdit(self)
        self.line_edit_user_name.resize(150, 30)
        self.line_edit_user_name.setPlaceholderText("请输入用户名")
        self.line_edit_user_name.setMaxLength(8)
        user_account_completer = QCompleter(self.user_account_list, self.line_edit_user_name)
        self.line_edit_user_name.setCompleter(user_account_completer)
        # What's this提示
        self.line_edit_user_name.setWhatsThis("请输入用户名")

        self.line_edit_user_password = QLineEdit(self)
        self.line_edit_user_password.resize(150, 30)
        self.line_edit_user_password.setPlaceholderText("请输入密码")
        self.line_edit_user_password.setMaxLength(8)
        self.line_edit_user_password.setClearButtonEnabled(True)
        # What's this提示
        self.line_edit_user_password.setWhatsThis("请输入密码")

    def push_button(self):
        self.login_submit_button = QPushButton(self)
        self.login_submit_button.setText("登录")
        # 工具提示
        self.login_submit_button.setToolTip("登录")
        self.login_submit_button.setEnabled(False)

        self.register_button = QPushButton(self)
        self.register_button.setText("注册")
        self.register_button.setToolTip("注册")

    def user_action(self):
        # 定义密码框显示模式动作
        line_edit_user_password_echo_mode = QAction(self.line_edit_user_password)

        def line_edit_user_password_echo_mode_switch():
            # 密码显示模式切换
            if self.line_edit_user_password.echoMode() == QLineEdit.Password:
                line_edit_user_password_echo_mode.setIcon(QIcon("echo_mode_text.png"))
                self.line_edit_user_password.setEchoMode(QLineEdit.Normal)
            else:
                line_edit_user_password_echo_mode.setIcon(QIcon("echo_mode_password.png"))
                self.line_edit_user_password.setEchoMode(QLineEdit.Password)

        # 首次运行即设置密码框显示模式
        line_edit_user_password_echo_mode_switch()
        # 添加密码框显示模式自定义动作
        self.line_edit_user_password.addAction(line_edit_user_password_echo_mode, QLineEdit.TrailingPosition)
        # 连接密码框显示模式信号与槽
        line_edit_user_password_echo_mode.triggered.connect(line_edit_user_password_echo_mode_switch)

        # !---用户名、密码文本框内容变动时,设置登录按钮可用状态及窗口编辑状态  方法1-----------------
        def line_edit_user_account_text_changed():
            if self.line_edit_user_name.text() and self.line_edit_user_password.text():
                self.login_submit_button.setEnabled(True)
                self.setWindowModified(True)
                self.label_user_name_tip.setVisible(False)
                self.label_user_password_tip.setVisible(False)
            elif self.line_edit_user_name.text() or self.line_edit_user_password.text():
                if self.line_edit_user_name.text():
                    self.label_user_name_tip.setVisible(False)
                else:
                    self.label_user_password_tip.setVisible(False)
                self.login_submit_button.setEnabled(False)
                self.setWindowModified(True)
            else:
                self.login_submit_button.setEnabled(False)
                self.setWindowModified(False)
            self.label_title.setText("请登录!")

        # 连接用户名、密码文本框内容变动信号与槽
        self.line_edit_user_name.textChanged.connect(line_edit_user_account_text_changed)
        self.line_edit_user_password.textChanged.connect(line_edit_user_account_text_changed)

        # ----------------------------------------------------------------

        # # !---用户名、密码文本框内容变动时,设置登录按钮可用状态及窗口编辑状态 方法2-----------------
        # def line_edit_user_name_text_changed(a0):
        #     if len(a0) > 0:
        #         self.line_edit_user_account_state["line_edit_user_name_state"] = True
        #     else:
        #         self.line_edit_user_account_state["line_edit_user_name_state"] = False
        #     self.label_user_name_tip.setVisible(False)
        #     line_edit_user_account_text_changed()
        #
        # def line_edit_user_password_text_changed(a0):
        #     if len(a0) > 0:
        #         self.line_edit_user_account_state["line_edit_user_password_state"] = True
        #     else:
        #         self.line_edit_user_account_state["line_edit_user_password_state"] = False
        #     self.label_user_password_tip.setVisible(False)
        #     line_edit_user_account_text_changed()
        #
        # def line_edit_user_account_text_changed():
        #     if self.line_edit_user_account_state["line_edit_user_name_state"] \
        #             and self.line_edit_user_account_state["line_edit_user_password_state"]:
        #         self.login_submit_button.setEnabled(True)
        #         self.setWindowModified(True)
        #     elif self.line_edit_user_account_state["line_edit_user_name_state"] \
        #             or self.line_edit_user_account_state["line_edit_user_password_state"]:
        #         self.login_submit_button.setEnabled(False)
        #         self.setWindowModified(True)
        #     else:
        #         self.login_submit_button.setEnabled(False)
        #         self.setWindowModified(False)
        #     self.label_title.setText("请登录!")
        #
        # # 连接用户名、密码文本框内容变动信号与槽
        # self.line_edit_user_name.textChanged.connect(line_edit_user_name_text_changed)
        # self.line_edit_user_password.textChanged.connect(line_edit_user_password_text_changed)
        #
        # # ----------------------------------------------------------------

        def line_edit_login_submit():
            user_name = self.line_edit_user_name.text()
            user_password = self.line_edit_user_password.text()
            user_account_check_result = AccountUtilities.check_account(user_name, user_password)
            if user_account_check_result == 0:
                self.label_title.setText("登录成功")
            # 用户名错误
            elif user_account_check_result == 1:
                self.line_edit_user_name.setText("")
                self.line_edit_user_password.setText("")
                self.label_title.setText("登录失败!")
                self.label_user_name_tip.setVisible(True)
                self.line_edit_user_name.setFocus()
            # 密码错误
            elif user_account_check_result == 2:
                self.line_edit_user_password.setText("")
                self.label_title.setText("登录失败!")
                self.label_user_password_tip.setVisible(True)
                self.line_edit_user_password.setFocus()

        self.login_submit_button.clicked.connect(line_edit_login_submit)

        def register_button_clicked():
            self.register_window = RegisterWindow()
            self.register_window.show()

        self.register_button.clicked.connect(register_button_clicked)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

发表回复 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

类别

  • Cat
  • Python
  • MySQL
  • Django
  • Html/CSS
  • JavaScript
  • Vue
  • RegExp
  • php
  • Practice
  • Virtualization
  • Linux
  • Windows
  • Android
  • NAS
  • Software
  • Hardware
  • Network
  • Router
  • Office
  • WordPress
  • SEO
  • English
  • Games
  • Recipes
  • living
  • Memorandum
  • Essays
  • 未分类

归档

©2015-2023 艾丽卡 Blog support@alaica.com
      ajax-loader