文本框是GUI界面中使用頻率較高的控件,文本框又分為單行文本框和多行文本框颅悉,本文先講單行文本框,單行文本框用途很廣迁匠,比如密碼賬號(hào)密碼框剩瓶、搜索欄驹溃、路徑地址欄等。
3.1簡(jiǎn)介
QLineEdit可以輸入和顯示文本信息延曙,同時(shí)可以設(shè)置顯示格式和文本框?qū)傩酝愫祝琎LineEdit類中常用的方法如下表所示。
QLineEdit類中常用信號(hào)如下:
textChanged(str)
只要文字發(fā)生變化就會(huì)發(fā)出此信號(hào)枝缔。當(dāng)通過調(diào)用setText()
以編程方式更改文本時(shí)布疙,也會(huì)發(fā)出此信號(hào),而editingFinished()
按下返回或回車鍵或線條編輯失去焦點(diǎn)時(shí)發(fā)出此信號(hào)愿卸。
3.2 QLineEdit實(shí)例
首先使用QT Designer拖幾個(gè)控件灵临,效果如下:
接下來就是實(shí)現(xiàn)到邏輯代碼。
# -*- coding: utf-8 -*-
"""
@file mainMainpage.py
@author BruceOu
@version V1.0
@date 2021-11-25
@blog https://blog.bruceou.cn/
@Official Accounts 嵌入式實(shí)驗(yàn)樓
@brief MainWindow
"""
from PyQt5 import QtWidgets
from PyQt5.QtGui import QGuiApplication, QIcon, QDesktopServices, QFont
from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox, QButtonGroup, QToolButton, QMenu, QAction
from PyQt5.QtCore import Qt,QUrl
import re
from ui.ui_mainpage import Ui_MainWindow
## MainWindow
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
#init
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setWindowTitle('QRadioButton')
self.setupUi(self)
self.show()
## login ###############################################################################################
## user
self.lineEditUser.setPlaceholderText('Please enter user name')
self.lineEditUser.setStyleSheet("color:red")
## set font
## 方法一
font = QFont()
font.setFamily('微軟雅黑')
font.setBold(True)
font.setPointSize(10)
font.setWeight(75)
self.lineEditUser.setFont(font)
## 方法二
# self.lineEditUser.setFont(QFont('微軟雅黑', 20))
## 設(shè)置用戶名的最大輸入為16
self.lineEditUser.setMaxLength(16)
## Password
self.lineEditPassword.setPlaceholderText('Please enter password')
self.lineEditPassword.setEchoMode(QtWidgets.QLineEdit.Password)
## PasswordNoecho
self.lineEditPasswordNoecho.setEchoMode(QtWidgets.QLineEdit.NoEcho)
self.lineEditPasswordNoecho.setPlaceholderText('Please enter password')
## PasswordEchoOnEdit
self.lineEditPasswordEchoOnEdit.setEchoMode(QtWidgets.QLineEdit.PasswordEchoOnEdit)
self.lineEditPasswordEchoOnEdit.setPlaceholderText('Please enter password')
## setting #############################################################################################
# IP
self.lineEditIP.setInputMask('000.000.000.000;_')
self.lineEditMask.setInputMask('000.000.000.000;_')
### event
self.lineEditPassword.editingFinished.connect(self.check_password)
self.pushButtonLogin.clicked.connect(self.btn_clicked) ## 綁定按鈕事件
self.pushButtonSetting.clicked.connect(self.btn_clicked)
def check_password(self):
"""
Brief
----------
check password
Parameters
----------
None
Returns
----------
None
"""
sender = self.sender()
passwd = sender.text()
if(len(passwd) < 6 or len(passwd) > 16):
QMessageBox.warning(self, 'Warning','The password should be 6 - 16!')
elif(None == re.match("^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])).*$" ,passwd)):
QMessageBox.warning(self, 'Warning','Password must contain uppercase and lowercase letters and numbers!')
else:
pass
def btn_clicked(self):
"""
Brief
----------
pushButton clicked
Parameters
----------
None
Returns
----------
None
"""
sender = self.sender()
if(sender == self.pushButtonLogin):
QMessageBox.about(self, 'Login','User: {}, Password: {}, PasswordNoecho: {}, PasswordEchoOnEdit: {}'\
.format(self.lineEditUser.text(), self.lineEditPassword.text(), self.lineEditPasswordNoecho.text(), self.lineEditPasswordEchoOnEdit.text()))
elif(sender == self.pushButtonSetting):
QMessageBox.about(self, 'Setting','IP: {}, Mask: {}'\
.format(self.lineEditIP.text(), self.lineEditMask.text()))
def closeEvent(self, event):
"""
Brief
----------
Close Event
Parameters
----------
event
Returns
----------
None
"""
event.accept()
【完整代碼參考附件QLineEdit】
演示效果:
代碼很簡(jiǎn)單擦酌,只是根據(jù)前面的API根據(jù)實(shí)際情況使用就可以了俱诸,下面就挑幾個(gè)講解了。
setPlaceholderText(str)
表示設(shè)置行文本的提示文字赊舶,設(shè)置此屬性將使行編輯顯示一個(gè)灰色的占位符文本睁搭,當(dāng)有輸入文字時(shí),提示文字就會(huì)消失笼平。
setInputMask()
用于設(shè)置掩碼园骆,主要在IP地址、License寓调、日期等地方用到锌唾,用于限定輸入格式。掩碼由掩碼字符與分隔符字符串組成夺英,后面可以跟一個(gè)分號(hào)和空白字符晌涕。
常用掩碼示例如下:
資源獲取方法
1.關(guān)注公眾號(hào)[AI實(shí)驗(yàn)樓]
2.在公眾號(hào)回復(fù)關(guān)鍵詞[PyQt5]獲取資料提取碼
歡迎訪問我的網(wǎng)站
BruceOu的嗶哩嗶哩
BruceOu的主頁
BruceOu的博客
BruceOu的CSDN博客
BruceOu的簡(jiǎn)書
BruceOu的知乎