您好,登錄后才能下訂單哦!
小編給大家分享一下PyQt5中QLineEdit控件常見的使用方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
這篇文章將為大家詳細講解有關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_())
以上是PyQt5中QLineEdit控件常見的使用方法的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。