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_())

添加格式校验:

例如:

 1 #QLineEdit控件使用
 2 import sys
 3 from  PyQt5.QtWidgets import QPushButton,QApplication,QMainWindow,QLineEdit,QFormLayout,QWidget,QLabel
 4 from PyQt5.QtGui import QIntValidator,QDoubleValidator,QRegExpValidator
 5 from PyQt5.QtCore import  QRegExp
 6 class QLineEditDemo(QWidget):
 7     def __init__(self,parent=None):
 8         super(QLineEditDemo,self).__init__(parent)
 9 
10         self.setWindowTitle("QLineEdit控件使用格式校验")
11         self.resize(500,600)
12         self.formLayout=QFormLayout()
13 
14         edit_int=QLineEdit()
15         edit_int.setPlaceholderText("请输入整数!")
16         #设置获取焦点
17         edit_int.setFocus()
18 
19         edit_float=QLineEdit()
20         edit_float.setPlaceholderText("请输入浮点数!")
21 
22         edit_chars= QLineEdit()
23         edit_chars.setPlaceholderText("请输入指定格式字符!")
24 
25 
26         self.formLayout.addRow("整数",edit_int)
27         self.formLayout.addRow("浮点型",edit_float)
28         self.formLayout.addRow("指定格式字符串", edit_chars)
29         #格式校验
30         intValidator=QIntValidator(self)
31         intValidator.setRange(1,200)
32 
33         doubleValidator=QDoubleValidator(self)
34         doubleValidator.setRange(-300,300)
35         doubleValidator.setNotation(QDoubleValidator.StandardNotation)
36         doubleValidator.setDecimals(2)
37 
38         reg=QRegExp("[a-zA-Z]{6,8}")
39         cValidator=QRegExpValidator(self)
40         cValidator.setRegExp(reg)
41 
42         edit_int.setValidator(intValidator)
43         edit_float.setValidator(doubleValidator)
44         edit_chars.setValidator(cValidator)
45 
46         self.setLayout(self.formLayout)
47 
48 
49 
50 if __name__=="__main__":
51     app=QApplication(sys.argv)
52     win=QLineEditDemo()
53     win.show()
54     sys.exit(app.exec_())

 

 

例如:IP、mac地址、日期等校验

 1 #QLineEdit格式校验
 2 #QLineEdit控件使用
 3 import sys
 4 from  PyQt5.QtWidgets import QPushButton,QApplication,QMainWindow,QLineEdit,QFormLayout,QWidget,QLabel
 5 from PyQt5.QtGui import QIntValidator,QDoubleValidator,QRegExpValidator
 6 from PyQt5.QtCore import  QRegExp
 7 class QLineEditDemo(QWidget):
 8     def __init__(self,parent=None):
 9         super(QLineEditDemo,self).__init__(parent)
10 
11         self.setWindowTitle("QLineEdit控件使用格式校验")
12         self.resize(500,600)
13         self.formLayout=QFormLayout()
14 
15         edit_num=QLineEdit()
16         edit_num.setPlaceholderText("数字掩码!")
17         #设置获取焦点
18         edit_num.setFocus()
19 
20         edit_mac=QLineEdit()
21         edit_mac.setPlaceholderText("mac掩码!")
22 
23         edit_date= QLineEdit()
24         edit_date.setPlaceholderText("日期掩码!")
25 
26         edit_str = QLineEdit()
27         edit_str.setPlaceholderText("许可证掩码!")
28 
29 
30         self.formLayout.addRow("数字掩码",edit_num)
31         self.formLayout.addRow("mac掩码",edit_mac)
32         self.formLayout.addRow("日期掩码", edit_date)
33         self.formLayout.addRow("许可证掩码", edit_str)
34         #格式校验
35         edit_num.setInputMask("000.000.000;_")
36         edit_mac.setInputMask("HH:HH:HH:HH:HH:HH;_")
37         edit_date.setInputMask("0000-00-00")
38         edit_str.setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#")
39 
40         self.setLayout(self.formLayout)
41 
42 
43 
44 if __name__=="__main__":
45     app=QApplication(sys.argv)
46     win=QLineEditDemo()
47     win.show()
48     sys.exit(app.exec_())

 

 

综合实例:例如

 1 #QLineEdit格式校验
 2 #QLineEdit控件使用
 3 import sys
 4 from  PyQt5.QtWidgets import QPushButton,QApplication,QMainWindow,QLineEdit,QFormLayout,QWidget,QLabel
 5 from PyQt5.QtGui import QIntValidator,QDoubleValidator,QRegExpValidator,QFont
 6 from PyQt5.QtCore import  QRegExp,Qt
 7 class QLineEditDemo(QWidget):
 8     def __init__(self,parent=None):
 9         super(QLineEditDemo,self).__init__(parent)
10 
11         self.setWindowTitle("QLineEdit控件使用格式校验")
12         self.resize(500,600)
13         self.formLayout=QFormLayout()
14 
15         edit_username = QLineEdit()
16         edit_username.setPlaceholderText("请输入用户名!")
17         # 设置获取焦点
18         edit_username.setFocus()
19         #设置字体
20         edit_username.setFont(QFont("微软雅黑",20))
21         edit_username.setAlignment(Qt.AlignRight)#靠右侧对齐
22 
23         edit_pwd = QLineEdit()
24         edit_pwd.setPlaceholderText("请输入密码!")
25         edit_pwd.editingFinished.connect(self.enterPress)
26 
27 
28         edit_sal = QLineEdit()
29         edit_sal.setPlaceholderText("请输入金额!")
30         edit_sal.textChanged.connect(self.getText)
31 
32         edit_phone=QLineEdit()
33         edit_phone.setPlaceholderText("Phone-Number")
34 
35         edit_readOnly= QLineEdit()
36         edit_readOnly.setText("190123")
37         edit_readOnly.setReadOnly(True)#设置只读模式
38 
39         # 设置显示效果
40         edit_username.setEchoMode(QLineEdit.Normal)
41         edit_pwd.setEchoMode(QLineEdit.Password)  # QLineEdit.PasswordEchoOnEdit,QLineEdit.Password,QLineEdit.NoEcho
42         edit_sal.setValidator(QDoubleValidator(0.99,99.99,2)) #限制输入小数点后两位
43         edit_phone.setInputMask("+99_9999_99999999")
44 
45         self.formLayout.addRow("UserName", edit_username)
46         self.formLayout.addRow("PWD", edit_pwd)
47         self.formLayout.addRow("Sal", edit_sal)
48         self.formLayout.addRow("Phone-Number", edit_phone)
49         self.setLayout(self.formLayout)
50 
51 
52     def getText(self,text):
53         print("输入的值为:"+text)
54         
55     def enterPress(self):
56         print("已经输入内容")
57 
58 
59 if __name__=="__main__":
60     app=QApplication(sys.argv)
61     win=QLineEditDemo()
62     win.show()
63     sys.exit(app.exec_())

 

posted @ 2018-12-03 16:11  zyg_100  阅读(9314)  评论(0编辑  收藏  举报