pyqt5基础教程(三),模拟登陆

上图是pyqt5做的一个模拟登录器

老样子先做界面,导入需要的库,我们的界面就用到的是Qtwidgets

from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon
import sys

导入之后,写个壳子存放控件

class LoginTest(QMainWindow):
    count = 0
    def __init__(self):
        super().__init__()
        self.mainwindow = QWidget()
        self.mainwindow.setFixedSize(400, 300)
        self.mainwindow.resize(400, 300)
        self.mainwindow.setWindowTitle('用户登陆')
        self.creat_widget()
        self.config()
        self.mainwindow.show()

这个解释一下:

class LoginTest(QMainWindow):#写的一个类,继承Qmainwinwos
    def __init__(self):
        super().__init__()
        self.mainwindow = QWidget()
        self.mainwindow.setFixedSize(400, 300)
        self.mainwindow.resize(400, 300)#锁定窗体大小,不可修改拖拉
        self.mainwindow.setWindowTitle('用户登陆')#标题
        self.mainwindow.show()#显示窗ti
if __name__ == '__main__':
    u = QApplication(sys.argv)
    app = LoginTest()
    sys.exit(u.exec_())

 

现在再创建窗体控件

 def creat_widget(self):
        self.Llogin=QLabel('用户名:',self.mainwindow)
        self.Llogin.setGeometry(20,50,80,30)
        self.Lpassword=QLabel('密码:',self.mainwindow)
        self.Lpassword.setGeometry(20,100,80,30)
        self.Elogin=QLineEdit(self.mainwindow)
        self.Elogin.setGeometry(100,50,180,30)
        self.Epassword=QLineEdit(self.mainwindow)
        self.Epassword.setGeometry(100,100,180,30)
        self.Blogin=QPushButton('确定',self.mainwindow)
        self.Blogin.setGeometry(80,200,100,35)
        self.Bcancel=QPushButton('取消',self.mainwindow)
        self.Bcancel.setGeometry(200,200,100,35)
        self.Lmes=QLabel(self.mainwindow)
        self.Lmes.setGeometry(100,160,180,30)

创建完后,我们需要设置控件触发以及样式颜色

    def config(self):
        self.mainwindow.setStyleSheet("background-color:lightgreen")
        self.Elogin.setStyleSheet("background-color:WhiteSmoke")
        self.Epassword.setStyleSheet("background-color:WhiteSmoke")
        self.Blogin.setStyleSheet("background-color:CadetBlue")
        self.Bcancel.setStyleSheet("background-color:CadetBlue")
        self.Blogin.clicked.connect(self.loginvalidata)
        self.Bcancel.clicked.connect(self.mainwindow.close)
        self.Epassword.setEchoMode(QLineEdit.Password)
        self.Elogin.setPlaceholderText('请输入用户名')
        self.Epassword.setPlaceholderText('请输入密码')
        self.Bsee.setObjectName('see')
        seestyle='#see{background-color:rgba(0, 0, 0, 0);background-image:url(res/eye2.png)}' \
                 '#see:Pressed{background-image:url(res/eye1.png)}'
        self.Bsee.setStyleSheet(seestyle)

此处需要注意:

修改控件颜色,举个栗子:

self.Elogin.setStyleSheet("background-color:WhiteSmoke") 将输入框背景色修改为WhiteSmoke色,颜色可以去RGB寻找

修改button为透明的可以这样改:

seestyle='#see{background-color:rgba(0, 0, 0, 0);background-image:url(res/eye2.png)}' \
         '#see:Pressed{background-image:url(res/eye1.png)}'

#see{background-color:rgba(0, 0, 0, 0)这段代码意思是设置名字为see的控件背景色为透明,

background-image:url(res/eye2.png)} 是设置背景图片是使用eye2.png

#see:Pressed{background-image:url(res/eye1.png)} 是点此图标时变换图标

再写判断函数

当点击小太阳次数过多就会弹出警示框,随后会自动关闭,

方法,首先定义计数器count=0

然后每次点击就自加一次,当数字达到某个值时再判断处理

    def show_erro(self):
        self.count += 1
        if self.count >= 6:
            QMessageBox.warning(self.mainwindow, '警告', '哥,不要玩了')
            exit()
        else:
            return

 

   def loginvalidata(self):
        if self.Elogin.text()=='admin' and self.Epassword.text()=='123456':
            print('登陆成功')
            self.Lmes.setText('用户:{}登陆成功'.format(self.Elogin.text()))
            return True
        else:
            QMessageBox.warning(self.mainwindow,'错误','用户名密码错误')

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值