pyqt 登录界面

43 篇文章 1 订阅

方式一,使用ui文件

import sys
from PyQt5.QtWidgets import QApplication, QMessageBox, QWidget
from PyQt5.uic import loadUi

# 这里需要继承 QWidget 类,因为 login.ui 是 QWidget 类型
class LoginWindow(QWidget):
    def __init__(self):
        super().__init__()
        loadUi(r'login.ui', self)
        self.add_signal()

    def add_signal(self):
        self.login_btn.clicked.connect(self.to_login)

    def to_login(self):
        uname = self.user_name.text().split()
        pwd = self.password.text().split()
        if uname == 'test' and pwd == '123456':
            pass
        else:
            QMessageBox.warning(self, '错误', '用户名或者密码错误!')


app = QApplication(sys.argv)
wind = LoginWindow()
wind.show()
app.exec()

方式二,使用py文件

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox
from login import Ui_Form

class LoginWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.add_signal()

    def add_signal(self):
        self.ui.login_btn.clicked.connect(self.to_login)

    def to_login(self):
        uname = self.ui.user_name.text().split()
        pwd = self.ui.password.text().split()
        if uname == 'test' and pwd == '123456':
            pass
        else:
            QMessageBox.warning(self, '错误', '用户名或者密码错误!')


app = QApplication(sys.argv)
wind = LoginWindow()
wind.show()
app.exec()


login.ui 文件

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>499</width>
    <height>483</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>login</string>
  </property>
  <widget class="QPushButton" name="login_btn">
   <property name="geometry">
    <rect>
     <x>190</x>
     <y>280</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>登录</string>
   </property>
  </widget>
  <widget class="QWidget" name="">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>150</y>
     <width>271</width>
     <height>31</height>
    </rect>
   </property>
   <layout class="QHBoxLayout" name="horizontalLayout" stretch="1,4">
    <item>
     <widget class="QLabel" name="label">
      <property name="text">
       <string>用户名</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QLineEdit" name="user_name"/>
    </item>
   </layout>
  </widget>
  <widget class="QWidget" name="">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>210</y>
     <width>271</width>
     <height>31</height>
    </rect>
   </property>
   <layout class="QHBoxLayout" name="horizontalLayout_2" stretch="1,4">
    <item>
     <widget class="QLabel" name="label_2">
      <property name="text">
       <string>密码</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QLineEdit" name="password">
      <property name="echoMode">
       <enum>QLineEdit::Password</enum>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

login.py 文件

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(499, 483)
        self.login_btn = QtWidgets.QPushButton(Form)
        self.login_btn.setGeometry(QtCore.QRect(190, 280, 75, 23))
        self.login_btn.setObjectName("login_btn")
        self.widget = QtWidgets.QWidget(Form)
        self.widget.setGeometry(QtCore.QRect(110, 150, 271, 31))
        self.widget.setObjectName("widget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label = QtWidgets.QLabel(self.widget)
        self.label.setObjectName("label")
        self.horizontalLayout.addWidget(self.label)
        self.user_name = QtWidgets.QLineEdit(self.widget)
        self.user_name.setObjectName("user_name")
        self.horizontalLayout.addWidget(self.user_name)
        self.horizontalLayout.setStretch(0, 1)
        self.horizontalLayout.setStretch(1, 4)
        self.widget1 = QtWidgets.QWidget(Form)
        self.widget1.setGeometry(QtCore.QRect(110, 210, 271, 31))
        self.widget1.setObjectName("widget1")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.widget1)
        self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.label_2 = QtWidgets.QLabel(self.widget1)
        self.label_2.setObjectName("label_2")
        self.horizontalLayout_2.addWidget(self.label_2)
        self.password = QtWidgets.QLineEdit(self.widget1)
        self.password.setEchoMode(QtWidgets.QLineEdit.Password)
        self.password.setObjectName("password")
        self.horizontalLayout_2.addWidget(self.password)
        self.horizontalLayout_2.setStretch(0, 1)
        self.horizontalLayout_2.setStretch(1, 4)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "login"))
        self.login_btn.setText(_translate("Form", "登录"))
        self.label.setText(_translate("Form", "用户名"))
        self.label_2.setText(_translate("Form", "密码"))

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值