《PyQT5软件开发 - 控件篇》第3章 单行文本框QLineEdit
文本框是GUI界面中使用頻率較高的控件,文本框又分為單行文本框和多行文本框,本文先講單行文本框,單行文本框用途很廣,比如密碼賬號密碼框、搜索欄、路徑地址欄等。
3.1簡介
QLineEdit可以輸入和顯示文本信息,同時可以設置顯示格式和文本框屬性,QLineEdit類中常用的方法如下表所示。
QLineEdit類中常用信號如下:
textChanged(str)只要文字發生變化就會發出此信號。當通過調用setText()以編程方式更改文本時,也會發出此信號,而editingFinished()按下返回或回車鍵或線條編輯失去焦點時發出此信號。
3.2 QLineEdit實例
首先使用QT Designer拖幾個控件,效果如下:
接下來就是實現到邏輯代碼。
# -*- coding: utf-8 -*- """ @file mainMainpage.py @author BruceOu @version V1.0 @date 2021-11-25 @blog https://blog.bruceou.cn/ @Official Accounts 嵌入式實驗樓 @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):#initdef __init__(self, parent=None):super(MainWindow, self).__init__(parent)self.setWindowTitle('QRadioButton')self.setupUi(self)self.show()## login ################################################################################################# userself.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))## 設置用戶名的最大輸入為16self.lineEditUser.setMaxLength(16)## Passwordself.lineEditPassword.setPlaceholderText('Please enter password')self.lineEditPassword.setEchoMode(QtWidgets.QLineEdit.Password)## PasswordNoechoself.lineEditPasswordNoecho.setEchoMode(QtWidgets.QLineEdit.NoEcho)self.lineEditPasswordNoecho.setPlaceholderText('Please enter password')## PasswordEchoOnEditself.lineEditPasswordEchoOnEdit.setEchoMode(QtWidgets.QLineEdit.PasswordEchoOnEdit)self.lineEditPasswordEchoOnEdit.setPlaceholderText('Please enter password')## setting ############################################################################################## IPself.lineEditIP.setInputMask('000.000.000.000;_')self.lineEditMask.setInputMask('000.000.000.000;_')### eventself.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 passwordParameters----------NoneReturns----------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:passdef btn_clicked(self):"""Brief----------pushButton clickedParameters----------NoneReturns----------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 EventParameters----------eventReturns----------None"""event.accept()【完整代碼參考附件QLineEdit】
演示效果:
代碼很簡單,只是根據前面的API根據實際情況使用就可以了,下面就挑幾個講解了。
setPlaceholderText(str)表示設置行文本的提示文字,設置此屬性將使行編輯顯示一個灰色的占位符文本,當有輸入文字時,提示文字就會消失。
setInputMask()用于設置掩碼,主要在IP地址、License、日期等地方用到,用于限定輸入格式。掩碼由掩碼字符與分隔符字符串組成,后面可以跟一個分號和空白字符。
常用掩碼示例如下:
資源獲取方法
1.掃描下面二維碼,關注公眾號[AI實驗樓]
2.在公眾號回復關鍵詞[PyQt5]獲取資料提取碼
歡迎訪問我的網站
BruceOu的嗶哩嗶哩
BruceOu的主頁
BruceOu的博客
BruceOu的CSDN博客
BruceOu的簡書
BruceOu的知乎
總結
以上是生活随笔為你收集整理的《PyQT5软件开发 - 控件篇》第3章 单行文本框QLineEdit的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: lorawan 科普文
- 下一篇: CSS学习笔记 01、CSS3基础知识学