您好,登录后才能下订单哦!
PyQt5-QLineEdit控件使用
QLineEdit类是一个单行文本框控件;
常用方法:
定义输入掩码的字符,如下列出了输入掩码的占位符和字面字符:
掩码实例如下;
常见信号:
例如:
#QLineEdit控件使用
import sys
from PyQt5.QtWidgets import QPushButton,QApplication,QMainWindow,QLineEdit,QFormLayout,QWidget,QLabel
class QLineEditDemo(QWidget):
def __init__(self,parent=None):
super(QLineEditDemo,self).__init__(parent)
self.setWindowTitle("QLineEdit控件使用")
self.resize(500,400)
self.formLayout=QFormLayout()
edit_username=QLineEdit()
edit_username.setPlaceholderText("请输入用户名!")
#设置获取焦点
edit_username.setFocus()
edit_pwd=QLineEdit()
edit_pwd.setPlaceholderText("请输入密码!")
#设置显示效果
edit_username.setEchoMode(QLineEdit.Normal)
edit_pwd.setEchoMode(QLineEdit.Password)#QLineEdit.PasswordEchoOnEdit,QLineEdit.Password,QLineEdit.NoEcho
self.formLayout.addRow("UserName",edit_username)
self.formLayout.addRow("PWD",edit_pwd)
self.setLayout(self.formLayout)
if __name__=="__main__":
app=QApplication(sys.argv)
win=QLineEditDemo()
win.show()
sys.exit(app.exec_())
添加格式校验:
例如:
#QLineEdit控件使用
import sys
from PyQt5.QtWidgets import QPushButton,QApplication,QMainWindow,QLineEdit,QFormLayout,QWidget,QLabel
from PyQt5.QtGui import QIntValidator,QDoubleValidator,QRegExpValidator
from PyQt5.QtCore import QRegExp
class QLineEditDemo(QWidget):
def __init__(self,parent=None):
super(QLineEditDemo,self).__init__(parent)
self.setWindowTitle("QLineEdit控件使用格式校验")
self.resize(500,600)
self.formLayout=QFormLayout()
edit_int=QLineEdit()
edit_int.setPlaceholderText("请输入整数!")
#设置获取焦点
edit_int.setFocus()
edit_float=QLineEdit()
edit_float.setPlaceholderText("请输入浮点数!")
edit_chars= QLineEdit()
edit_chars.setPlaceholderText("请输入指定格式字符!")
self.formLayout.addRow("整数",edit_int)
self.formLayout.addRow("浮点型",edit_float)
self.formLayout.addRow("指定格式字符串", edit_chars)
#格式校验
intValidator=QIntValidator(self)
intValidator.setRange(1,200)
doubleValidator=QDoubleValidator(self)
doubleValidator.setRange(-300,300)
doubleValidator.setNotation(QDoubleValidator.StandardNotation)
doubleValidator.setDecimals(2)
reg=QRegExp("[a-zA-Z]{6,8}")
cValidator=QRegExpValidator(self)
cValidator.setRegExp(reg)
edit_int.setValidator(intValidator)
edit_float.setValidator(doubleValidator)
edit_chars.setValidator(cValidator)
self.setLayout(self.formLayout)
if __name__=="__main__":
app=QApplication(sys.argv)
win=QLineEditDemo()
win.show()
sys.exit(app.exec_())
例如:IP、mac地址、日期等校验
#QLineEdit格式校验
#QLineEdit控件使用
import sys
from PyQt5.QtWidgets import QPushButton,QApplication,QMainWindow,QLineEdit,QFormLayout,QWidget,QLabel
from PyQt5.QtGui import QIntValidator,QDoubleValidator,QRegExpValidator
from PyQt5.QtCore import QRegExp
class QLineEditDemo(QWidget):
def __init__(self,parent=None):
super(QLineEditDemo,self).__init__(parent)
self.setWindowTitle("QLineEdit控件使用格式校验")
self.resize(500,600)
self.formLayout=QFormLayout()
edit_num=QLineEdit()
edit_num.setPlaceholderText("数字掩码!")
#设置获取焦点
edit_num.setFocus()
edit_mac=QLineEdit()
edit_mac.setPlaceholderText("mac掩码!")
edit_date= QLineEdit()
edit_date.setPlaceholderText("日期掩码!")
edit_str = QLineEdit()
edit_str.setPlaceholderText("许可证掩码!")
self.formLayout.addRow("数字掩码",edit_num)
self.formLayout.addRow("mac掩码",edit_mac)
self.formLayout.addRow("日期掩码", edit_date)
self.formLayout.addRow("许可证掩码", edit_str)
#格式校验
edit_num.setInputMask("000.000.000;_")
edit_mac.setInputMask("HH:HH:HH:HH:HH:HH;_")
edit_date.setInputMask("0000-00-00")
edit_str.setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#")
self.setLayout(self.formLayout)
if __name__=="__main__":
app=QApplication(sys.argv)
win=QLineEditDemo()
win.show()
sys.exit(app.exec_())
综合实例:例如
#QLineEdit格式校验
#QLineEdit控件使用
import sys
from PyQt5.QtWidgets import QPushButton,QApplication,QMainWindow,QLineEdit,QFormLayout,QWidget,QLabel
from PyQt5.QtGui import QIntValidator,QDoubleValidator,QRegExpValidator,QFont
from PyQt5.QtCore import QRegExp,Qt
class QLineEditDemo(QWidget):
def __init__(self,parent=None):
super(QLineEditDemo,self).__init__(parent)
self.setWindowTitle("QLineEdit控件使用格式校验")
self.resize(500,600)
self.formLayout=QFormLayout()
edit_username = QLineEdit()
edit_username.setPlaceholderText("请输入用户名!")
# 设置获取焦点
edit_username.setFocus()
#设置字体
edit_username.setFont(QFont("微软雅黑",20))
edit_username.setAlignment(Qt.AlignRight)#靠右侧对齐
edit_pwd = QLineEdit()
edit_pwd.setPlaceholderText("请输入密码!")
edit_pwd.editingFinished.connect(self.enterPress)
edit_sal = QLineEdit()
edit_sal.setPlaceholderText("请输入金额!")
edit_sal.textChanged.connect(self.getText)
edit_phone=QLineEdit()
edit_phone.setPlaceholderText("Phone-Number")
edit_readOnly= QLineEdit()
edit_readOnly.setText("190123")
edit_readOnly.setReadOnly(True)#设置只读模式
# 设置显示效果
edit_username.setEchoMode(QLineEdit.Normal)
edit_pwd.setEchoMode(QLineEdit.Password) # QLineEdit.PasswordEchoOnEdit,QLineEdit.Password,QLineEdit.NoEcho
edit_sal.setValidator(QDoubleValidator(0.99,99.99,2)) #限制输入小数点后两位
edit_phone.setInputMask("+99_9999_99999999")
self.formLayout.addRow("UserName", edit_username)
self.formLayout.addRow("PWD", edit_pwd)
self.formLayout.addRow("Sal", edit_sal)
self.formLayout.addRow("Phone-Number", edit_phone)
self.setLayout(self.formLayout)
def getText(self,text):
print("输入的值为:"+text)
def enterPress(self):
print("已经输入内容")
if __name__=="__main__":
app=QApplication(sys.argv)
win=QLineEditDemo()
win.show()
sys.exit(app.exec_())