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]+$")
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)
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
