小編給大家分享一下PyQt5中QLineEdit控件常見的使用方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
燈塔ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!這篇文章將為大家詳細(xì)講解有關(guān)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("請輸入用戶名!")
#設(shè)置獲取焦點
edit_username.setFocus()
edit_pwd=QLineEdit()
edit_pwd.setPlaceholderText("請輸入密碼!")
#設(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_())
添加格式校驗:
例如:
#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("請輸入整數(shù)!")
#設(shè)置獲取焦點
edit_int.setFocus()
edit_float=QLineEdit()
edit_float.setPlaceholderText("請輸入浮點數(shù)!")
edit_chars= QLineEdit()
edit_chars.setPlaceholderText("請輸入指定格式字符!")
self.formLayout.addRow("整數(shù)",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("數(shù)字掩碼!")
#設(shè)置獲取焦點
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)
#格式校驗
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("請輸入用戶名!")
# 設(shè)置獲取焦點
edit_username.setFocus()
#設(shè)置字體
edit_username.setFont(QFont("微軟雅黑",20))
edit_username.setAlignment(Qt.AlignRight)#靠右側(cè)對齊
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)#設(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ù)點后兩位
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控件常見的使用方法-創(chuàng)新互聯(lián)
轉(zhuǎn)載源于:http://aaarwkj.com/article2/cchiic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、ChatGPT、營銷型網(wǎng)站建設(shè)、微信小程序、服務(wù)器托管、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)