您好,登錄后才能下訂單哦!
小編給大家分享一下PyQt5中QLineEdit控件常見(jiàn)的使用方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
這篇文章將為大家詳細(xì)講解有關(guān)PyQt5中QLineEdit控件常見(jiàn)的使用方法,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
QLineEdit類(lèi)是一個(gè)單行文本框控件;
常用方法:
定義輸入掩碼的字符,如下列出了輸入掩碼的占位符和字面字符:
掩碼實(shí)例如下;
常見(jiàn)信號(hào):
例如:
#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("請(qǐng)輸入用戶名!")
#設(shè)置獲取焦點(diǎn)
edit_username.setFocus()
edit_pwd=QLineEdit()
edit_pwd.setPlaceholderText("請(qǐng)輸入密碼!")
#設(shè)置顯示效果
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_())
添加格式校驗(yàn):
例如:
#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控件使用格式校驗(yàn)")
self.resize(500,600)
self.formLayout=QFormLayout()
edit_int=QLineEdit()
edit_int.setPlaceholderText("請(qǐng)輸入整數(shù)!")
#設(shè)置獲取焦點(diǎn)
edit_int.setFocus()
edit_float=QLineEdit()
edit_float.setPlaceholderText("請(qǐng)輸入浮點(diǎn)數(shù)!")
edit_chars= QLineEdit()
edit_chars.setPlaceholderText("請(qǐng)輸入指定格式字符!")
self.formLayout.addRow("整數(shù)",edit_int)
self.formLayout.addRow("浮點(diǎn)型",edit_float)
self.formLayout.addRow("指定格式字符串", edit_chars)
#格式校驗(yàn)
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地址、日期等校驗(yàn)
#QLineEdit格式校驗(yàn)
#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控件使用格式校驗(yàn)")
self.resize(500,600)
self.formLayout=QFormLayout()
edit_num=QLineEdit()
edit_num.setPlaceholderText("數(shù)字掩碼!")
#設(shè)置獲取焦點(diǎn)
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("數(shù)字掩碼",edit_num)
self.formLayout.addRow("mac掩碼",edit_mac)
self.formLayout.addRow("日期掩碼", edit_date)
self.formLayout.addRow("許可證掩碼", edit_str)
#格式校驗(yàn)
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_())
綜合實(shí)例:例如
#QLineEdit格式校驗(yàn)
#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控件使用格式校驗(yàn)")
self.resize(500,600)
self.formLayout=QFormLayout()
edit_username = QLineEdit()
edit_username.setPlaceholderText("請(qǐng)輸入用戶名!")
# 設(shè)置獲取焦點(diǎn)
edit_username.setFocus()
#設(shè)置字體
edit_username.setFont(QFont("微軟雅黑",20))
edit_username.setAlignment(Qt.AlignRight)#靠右側(cè)對(duì)齊
edit_pwd = QLineEdit()
edit_pwd.setPlaceholderText("請(qǐng)輸入密碼!")
edit_pwd.editingFinished.connect(self.enterPress)
edit_sal = QLineEdit()
edit_sal.setPlaceholderText("請(qǐng)輸入金額!")
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)#設(shè)置只讀模式
# 設(shè)置顯示效果
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)) #限制輸入小數(shù)點(diǎn)后兩位
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("已經(jīng)輸入內(nèi)容")
if __name__=="__main__":
app=QApplication(sys.argv)
win=QLineEditDemo()
win.show()
sys.exit(app.exec_())
以上是PyQt5中QLineEdit控件常見(jiàn)的使用方法的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。