pyside6项目,创建一个简易的账号密码输入交互界面

       一个简单的Python代码示例,用于创建一个基本的账号密码输入交互界面。这个程序会要求用户输入账号和密码,并检查它们是否与预定义的账号和密码匹配。

第一步:使用qtdesinger.exe 绘制账号、密码界面;比较简单直接上图片;

保存文件,如登录框.ui

第二步:将登录框.ui文件转成py文件,这里使用pyside6 ,可以在CMD命令行使用命令转,

使用命令pyside6-uic 登录框.ui -o 登录框.py ,可以使用pycharm配置工具,点击转换即可;

图片为配置内容;

转换后的py代码如下:

# -*- coding: utf-8 -*-

################################################################################
## Form generated from reading UI file '登录框.ui'
##
## Created by: Qt User Interface Compiler version 6.6.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
    QFont, QFontDatabase, QGradient, QIcon,
    QImage, QKeySequence, QLinearGradient, QPainter,
    QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QHBoxLayout, QLabel, QLineEdit,
    QPushButton, QSizePolicy, QWidget)

class Ui_Form(object):
    def setupUi(self, Form):
        if not Form.objectName():
            Form.setObjectName(u"Form")
        Form.resize(244, 186)
        self.lineEdit = QLineEdit(Form)
        self.lineEdit.setObjectName(u"lineEdit")
        self.lineEdit.setGeometry(QRect(42, 76, 166, 25))
        font = QFont()
        font.setPointSize(12)
        self.lineEdit.setFont(font)
        self.label = QLabel(Form)
        self.label.setObjectName(u"label")
        self.label.setGeometry(QRect(4, 76, 32, 21))
        self.label.setFont(font)
        self.pushButton = QPushButton(Form)
        self.pushButton.setObjectName(u"pushButton")
        self.pushButton.setGeometry(QRect(70, 120, 91, 21))
        self.pushButton.setFont(font)
        self.widget = QWidget(Form)
        self.widget.setObjectName(u"widget")
        self.widget.setGeometry(QRect(3, 42, 206, 27))
        self.widget.setFont(font)
        self.horizontalLayout = QHBoxLayout(self.widget)
        self.horizontalLayout.setObjectName(u"horizontalLayout")
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.label_2 = QLabel(self.widget)
        self.label_2.setObjectName(u"label_2")
        self.label_2.setFont(font)

        self.horizontalLayout.addWidget(self.label_2)

        self.lineEdit_2 = QLineEdit(self.widget)
        self.lineEdit_2.setObjectName(u"lineEdit_2")
        self.lineEdit_2.setFont(font)

        self.horizontalLayout.addWidget(self.lineEdit_2)


        self.retranslateUi(Form)

        QMetaObject.connectSlotsByName(Form)
    # setupUi

    def retranslateUi(self, Form):
        Form.setWindowTitle(QCoreApplication.translate("Form", u"\u767b\u5f55\u6846", None))
#if QT_CONFIG(tooltip)
        Form.setToolTip(QCoreApplication.translate("Form", u"\u70b9\u6211\u6709\u60ca\u559c", None))
#endif // QT_CONFIG(tooltip)
        self.lineEdit.setPlaceholderText(QCoreApplication.translate("Form", u"\u8bf7\u8f93\u5165\u5bc6\u7801", None))
        self.label.setText(QCoreApplication.translate("Form", u"\u5bc6\u7801", None))
        self.pushButton.setText(QCoreApplication.translate("Form", u"\u786e\u8ba4", None))
        self.label_2.setText(QCoreApplication.translate("Form", u"\u767b\u5f55", None))
        self.lineEdit_2.setPlaceholderText(QCoreApplication.translate("Form", u"\u8bf7\u8f93\u5165\u8d26\u53f7", None))
    # retranslateUi
第四步:编写主程序;

# 导入必要的模块  
from PySide6.QtWidgets import QApplication, QWidget, QMessageBox  
import sys  
from login import Ui_Form  
  
# 定义 Mywindow 类,继承自 Ui_Form 和 QWidget  
class Mywindow(Ui_Form, QWidget):  
    # 初始化函数  
    def __init__(self):  
        super().__init__()  
        self.setupUi(self)  
        # 连接按钮点击事件到 loginFuc 函数  
        self.pushButton.clicked.connect(self.loginFuc)  
  
    # 定义 loginFuc 函数  
    def loginFuc(self):  
        # 获取账号输入框的值  
        account = self.lineEdit_2.text()  
        # 获取密码输入框的值  
        password = self.lineEdit.text()  
  
        # 如果账号或密码为空,则显示警告信息  
        if not account or not password:  
            QMessageBox.warning(self, "提示信息", "请输入账号和密码")  
            return  
        # 如果账号和密码均为 '123',则显示成功信息并打印 '登录成功'  
        if account == '123' and password == "123":  
            QMessageBox.information(self, '提示信息', '登录成功')  
            print('登录成功')  
        else:  
            # 其他情况则显示失败信息并打印 '登录失败'  
            QMessageBox.information(self, '提示信息', '登录失败,请输入正确的用户名和密码')  
            print('登录失败')  
  
# 主函数  
if __name__ == '__main__':  
    # 创建应用程序实例  
    app = QApplication()  
    # 创建 Mywindow 窗口实例并显示  
    window = Mywindow()  
    window.show()  
    # 运行应用程序,等待事件循环结束,然后退出应用程序  
    sys.exit(app.exec())

第五步:运行结果如下;

  • 38
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值