• PyQt5 QLineEdit


    QLineEdit常用方法及属性

    在这里插入图片描述

    QLineEdit 实例1

    import sys
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    
    class MyLineEditWindow(QWidget):
        def __init__(self, parent=None):
            super(MyLineEditWindow, self).__init__(parent)
            self.setWindowTitle("LineEdit Demo1")
            self.initUI()
    
        def initUI(self):
            flayout = QFormLayout()
    
            pNormalLineEdit = QLineEdit()
            pNoEchoLineEdit = QLineEdit()
            pPasswordLineEdit = QLineEdit()
            pPasswordEchoOnLineEdit = QLineEdit()
    
            pNormalLineEdit.setPlaceholderText("Normal")
            pNoEchoLineEdit.setPlaceholderText("NoEcho")
            pPasswordLineEdit.setPlaceholderText("Password")
            pPasswordEchoOnLineEdit.setPlaceholderText("PasswordEchoOn")
    
            pNormalLineEdit.setEchoMode(QLineEdit.Normal)
            pNoEchoLineEdit.setEchoMode(QLineEdit.NoEcho)
            pPasswordLineEdit.setEchoMode(QLineEdit.Password)
            pPasswordEchoOnLineEdit.setEchoMode(QLineEdit.PasswordEchoOnEdit)
    
            flayout.addRow("Normal", pNormalLineEdit)
            flayout.addRow("NoEcho", pNoEchoLineEdit)
            flayout.addRow("Password", pPasswordLineEdit)
            flayout.addRow("PasswordEchoOn", pPasswordEchoOnLineEdit)
    
            self.setLayout(flayout)
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        win = MyLineEditWindow()
        win.show()
        sys.exit(app.exec_())
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    在这里插入图片描述

    QLineEdit 实例2

    import sys
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    
    class MyLineEditWindow(QWidget):
        def __init__(self, parent=None):
            super(MyLineEditWindow, self).__init__(parent)
            self.setWindowTitle("QLineEdit Demo2")
            self.initUI()
    
        def initUI(self):
            fromLayout = QFormLayout()
    
            pIntLineEdit = QLineEdit()
            pDoubleLineEdit = QLineEdit()
            pValidatorLineEdit = QLineEdit()
    
            fromLayout.addRow("整型", pIntLineEdit)
            fromLayout.addRow("浮点型", pDoubleLineEdit)
            fromLayout.addRow("字母和数字", pValidatorLineEdit)
    
            pIntLineEdit.setPlaceholderText("整型")
            pDoubleLineEdit.setPlaceholderText("浮点型")
            pValidatorLineEdit.setPlaceholderText("字母和数字")
    
            pIntValidator = QIntValidator(self)
            pIntValidator.setRange(1, 99)
    
            pDoubleValidator = QDoubleValidator(self)
            pDoubleValidator.setRange(-360, 360)
            pDoubleValidator.setNotation(QDoubleValidator.StandardNotation)
            pDoubleValidator.setDecimals(2)
    
            reg = QRegExp("[a-zA-Z0-9]+$")  # [a-zA-Z0-9]:只能输入一个字符,[a-zA-Z0-9]+$:可以输入字符串
            pValidator = QRegExpValidator(self)
            pValidator.setRegExp(reg)
    
            # 设置验证器
            pIntLineEdit.setValidator(pIntValidator)
            pDoubleLineEdit.setValidator(pDoubleValidator)
            pValidatorLineEdit.setValidator(pValidator)
    
            self.setLayout(fromLayout)
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        win = MyLineEditWindow()
        win.show()
        sys.exit(app.exec_())
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    在这里插入图片描述

    QLineEdit 实例3

    import sys
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    
    class MyLineEditWindow(QWidget):
        def __init__(self, parent=None):
            super(MyLineEditWindow, self).__init__(parent)
            self.setWindowTitle("QLineEdit Demo3")
            self.initUI()
    
        def initUI(self):
            formLayout = QFormLayout()
    
            pIPLineEdit = QLineEdit()
            pMACLineEdit = QLineEdit()
            pDataLineEdit = QLineEdit()
            pLicenseLineEdit = QLineEdit()
    
            pIPLineEdit.setInputMask("000.000.000.000;_")
            pMACLineEdit.setInputMask("HH:HH:HH:HH:HH:HH;0")
            pDataLineEdit.setInputMask("0000-00-00")
            pLicenseLineEdit.setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#") 
    
            formLayout.addRow("IP", pIPLineEdit)
            formLayout.addRow("MAC", pMACLineEdit)
            formLayout.addRow("Data", pDataLineEdit)
            formLayout.addRow("License", pLicenseLineEdit)
    
            self.setLayout(formLayout)
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        win = MyLineEditWindow()
        win.show()
        sys.exit(app.exec_())
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    在这里插入图片描述

    QLineEdit 综合示例

    import sys
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    
    
    class MyLineEditWindow(QWidget):
        def __init__(self, parent=None):
            super(MyLineEditWindow, self).__init__(parent)
            e1 = QLineEdit()
            e1.setValidator(QIntValidator())
            e1.setMaxLength(4)
            e1.setAlignment(Qt.AlignRight)
            e1.setFont(QFont("Arial", 20))
    
            e2 = QLineEdit()
            e2.setValidator(QDoubleValidator(0.99, 99.99, 2))
    
            flo = QFormLayout()
            flo.addRow("integer validator", e1)
            flo.addRow("double validator", e2)
    
            e3 = QLineEdit()
            e3.setInputMask("+99_9999_99999")
            flo.addRow("input mask", e3)
    
            e4 = QLineEdit()
            e4.textChanged.connect(self.textchanged)
            # e4.textChanged.connect(self.textchanged(e4.text()))
            flo.addRow("text changed", e4)
    
            e5 = QLineEdit()
            e5.setEchoMode(QLineEdit.Password)
            e5.editingFinished.connect(self.enterPress)
            flo.addRow("password", e5) 
    
            e6 = QLineEdit("Hello world")
            e6.setReadOnly(True)
            flo.addRow("read only", e6)
    
            self.setLayout(flo)
            self.setWindowTitle("QLineEdit 综合示例")
            
    
        def textchanged(self, text):
            print("输入的内容为:" + text)
    
        def enterPress(self):
            print("已输入值")
    
        
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        win = MyLineEditWindow()
        win.show()
        sys.exit(app.exec_())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    在这里插入图片描述

  • 相关阅读:
    IDEA插件JProfiler安装使用
    【C语言】分支结构
    iOS 判断触摸位置是否在图片的透明区域
    CentOS7二进制安装和YUM安装mongodb,服务器无法安装5.0以上的 mongodb 数据库报错 Illegal instruction
    Arduino应用开发——通过小爱同学控制灯光
    MP157-2-TF-A移植:
    如何合并pdf文件,pdf合并教程
    TorchDynamo初探:Python ByteCode的动态修改
    Ubuntu新机配置
    python实现全局变量共享,一个全局变量在多个文件中使用
  • 原文地址:https://blog.csdn.net/u013420428/article/details/127939306