用python的pyqt5写登陆界面

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton


class Login(QWidget):
    def __init__(self):
        super().__init__()
        
        # 设置窗口标题
        self.setWindowTitle('登录')
        
        # 设置窗口尺寸
        self.setGeometry(400, 200, 300, 200)
        
        # 设置标签
        self.label_username = QLabel(self)
        self.label_username.setText('用户名:')
        self.label_username.move(50, 50)

        self.label_password = QLabel(self)
        self.label_password.setText('密码:')
        self.label_password.move(50, 90)

        # 设置文本框
        self.text_username = QLineEdit(self)
        self.text_username.move(100, 50)

        self.text_password = QLineEdit(self)
        self.text_password.setEchoMode(QLineEdit.Password)
        self.text_password.move(100, 90)

        # 设置登录按钮
        self.button_login = QPushButton('登录', self)
        self.button_login.move(100, 130)
        self.button_login.clicked.connect(self.handle_login)

    def handle_login(self):
        username = self.text_username.text()
        password = self.text_password.text()

        if username == 'admin' and password == '123456':
            print('登录成功!')
        else:
            print('用户名或密码错误!')


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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,您可以按照以下步骤使用PythonPyQt5编一个简单的登录界面: 1. 首先,导入必要的PyQt5模块: ``` from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton ``` 2. 创建一个QWidget窗口对象,并设置窗口的大小和标题: ``` app = QApplication([]) window = QWidget() window.setWindowTitle("登录") window.resize(300, 150) ``` 3. 在窗口上添加标签、文本框和按钮等控件: ``` label_username = QLabel('用户名:', window) label_username.move(20, 20) text_username = QLineEdit(window) text_username.move(100, 20) label_password = QLabel('密码:', window) label_password.move(20, 50) text_password = QLineEdit(window) text_password.setEchoMode(QLineEdit.Password) text_password.move(100, 50) btn_login = QPushButton('登录', window) btn_login.move(100, 100) ``` 4. 定义按钮点击事件,判断输入的用户名和密码是否正确: ``` def login(): username = text_username.text().strip() password = text_password.text().strip() if not username: label_username.setText('<font color="red">用户名不能为空</font>') return if not password: label_password.setText('<font color="red">密码不能为空</font>') return if username == 'admin' and password == 'admin': label_username.setText('') label_password.setText('') print('登录成功') else: label_username.setText('<font color="red">用户名或密码错误</font>') label_password.setText('') btn_login.clicked.connect(login) ``` 5. 最后,显示窗口并运行Qt应用程序: ``` window.show() app.exec_() ``` 完整的代码如下所示: ``` from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton app = QApplication([]) window = QWidget() window.setWindowTitle("登录") window.resize(300, 150) label_username = QLabel('用户名:', window) label_username.move(20, 20) text_username = QLineEdit(window) text_username.move(100, 20) label_password = QLabel('密码:', window) label_password.move(20, 50) text_password = QLineEdit(window) text_password.setEchoMode(QLineEdit.Password) text_password.move(100, 50) btn_login = QPushButton('登录', window) btn_login.move(100, 100) def login(): username = text_username.text().strip() password = text_password.text().strip() if not username: label_username.setText('<font color="red">用户名不能为空</font>') return if not password: label_password.setText('<font color="red">密码不能为空</font>') return if username == 'admin' and password == 'admin': label_username.setText('') label_password.setText('') print('登录成功') else: label_username.setText('<font color="red">用户名或密码错误</font>') label_password.setText('') btn_login.clicked.connect(login) window.show() app.exec_() ``` 希望这可以回答您的问题,如果您有任何其他问题,请继续提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值