我们在前面学习了各种按钮控件,从这一章开始就是各种输入控件的学习。

首先要用的就是QLineEdit——单行编辑器,

一描述

QLineEdit是一个单行文本编辑器,允许用户输入和编辑单行纯文本。自带一些编辑例如撤销、重做、剪切、粘贴等功能。

QLineEdit继承自QWidget类,具备父类各种API。

二.功能作用

1.创建控件,设置、获取文本

这个很简单,没啥说的

le = QLineEdit(window)
le = QLineEdit('默认字符',window)    #控件创建
le.setText()                        #设置字符串
le.insert('插入的字符串')             #从光标处插入字符串  
le.text()                           #获取真是的文本字符
le.displayText()                    #获取用户能看到的字符串

注意的是最后两个,如果控件是密码输入框的时候,输入的字符用户看的是*或者黑点。那用最后一个获取的字符就是你看到的字符。而用le.text()获得的就是真是的文本。

2.输出模式

文本框的文本输出模式按下面的枚举值看有这四种

QLineEdit.setEchoMode(QLineEdit.Normal)  #设置输出模式
QLineEdit.echoMode()                     #获取输出模式(int)
#输出模式枚举值
QLineEdit.Normal                      #正常显示,返回值0
QLineEdit.NoEcho                      #不显示,返回值1
QLineEdit.Password                    #密文显示,返回值2
QLineEdit.PasswordEchoOnEdit          #编辑时正常显示,失去焦点后变密文

3.占位提示符

有些时候在打开一个界面时对话框是有提示文本的,当输入内容后文本消失被输入的值替代,这就是占位提示符的作用

GUI学习之十——QLineEdit的学习总结

占位提示符的用法

QLineEdit.setPlaceholderText('占位文本')    #设置占位文本
QLineEdit.placeholderText()                #获取占位文本

4清空按钮

平时不显示,有字符时会出现一个‘叉叉

'GUI学习之十——QLineEdit的学习总结

单机叉叉后清除所有文本。就是清空按钮的作用

QLineEdit.setClearButtonEnabled(True)  #设置清空按钮开启
QLineEdit.isClearButtonEnabled()       #获取是否开启清空按钮

结合上面的用法可以做第一个案例:登陆对话框,要求如下

a.两个对话框,一个输入账号,一个输入密码。

b.账号框为明文,密码框用密文

c.测试用账户名为abc,密码为123,账户不存在时清空两个文本框,密码错误时清空密码框

d.增加标签,显示登陆情况

e.用面向对象的方式编程。

from PyQt5.Qt import *
from PyQt5 import QtGui
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(500,800)
        self.setMinimumSize(300,300)     #登陆窗口最小尺寸
        self.UI_test()


    def UI_test(self):
        self.le_account = QLineEdit(self)
        self.le_account.move(self.width() / 2 - self.le_account.width()/2, self.height() / 2 - 50)
        # self.le_account.move(0,0)
        self.le_account.resize(200,30)
        self.le_account.setPlaceholderText('请输入账号')
        self.le_password = QLineEdit(self)
        self.le_password.move(self.width() / 2 - self.le_password.width()/2, self.height() / 2)
        self.le_password.setEchoMode(QLineEdit.Password)
        self.le_password.setPlaceholderText('请输入密码')
        self.le_password.resize(200, 30)
        self.btn = QPushButton('登陆',self)
        self.btn.resize(100,30)
        self.btn.move(self.width() / 2 -self.btn.width()/2, self.height() / 2 + 50)
        self.btn.clicked.connect(self.checkin)
        self.label = QLabel(self)
        self.label.resize(150,50)
        self.label.move(self.width()/2,self.height()/2+100)
        self.label.setText('请登陆')
        self.label.setStyleSheet('background-color:cyan')

    def resizeEvent(self, a0: QtGui.QResizeEvent):
        print(self.size())
        self.le_account.move(self.width() / 2 - self.le_account.width()/2, self.height() / 2 - 50)
        self.le_password.move(self.width() / 2 - self.le_password.width()/2, self.height() / 2)
        self.le_password.setEchoMode(QLineEdit.Password)
        self.btn.move(self.width() / 2 -self.btn.width()/2, self.height() / 2 + 50)
        self.label.move(self.width() / 2-self.label.width()/2, self.height() / 2 + 100)
        print(self.le_account.pos())
        print(self.le_account.size())
    def checkin(self):
        account = self.le_account.text()
        pwd = self.le_password.text()
        if account != 'abc':
            self.le_account.setText('')
            self.le_password.setText('')
            self.label.setText('账号不存在')
        elif pwd != '123':
            self.le_password.setText('')
            self.label.setText('密码错误')
        else:
            self.label.setText('登陆成功')
        self.label.adjustSize()
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
案例一:登陆界面第一版

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-26
  • 2021-09-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案