This article covers the QLineEdit widget in PyQt.
Every GUI needs a way of taking input from the User. In PyQt, the most common way of taking input is through the QLineEdit widget. It offers you a single line where you can input Text.
If you wish for a way to get Multi-Line Text input, you will have to use the QTextEdit widget instead.
You will find a complete list of functions and options available for the QLineEdit here in this article.
Creating a QLineEdit Widget
Below is the simplest implementation of the QLineEdit Widget.
QtWidgets.QLineEdit() function is used to create a
QLineEdit() widget by passing as parameter the window obect we created earlier.
from PyQt5 import QtWidgets from PyQt5.QtWidgets import QApplication, QMainWindow import sys def show(): print(line.text()) app = QApplication(sys.argv) win = QMainWindow() win.setGeometry(400,400,300,300) win.setWindowTitle("CodersLegacy") line = QtWidgets.QLineEdit(win) line.move(100,80) win.show() sys.exit(app.exec_())
In it’s base form,
QLineEdit is very plain and simple. However there are over a dozen different methods that can be used to enhance it’s functionality as you’ll see below.
All examples after this well have the imports and setup code for the PyQt removed to decrease the amount of repeated code.
Retrieving QLineEdit values
In order to retrieve the entered text from the
QLineEdit widget, you have to use the
text() method on it. There are many different ways to use this method, either with the use of
QLineEdit‘s inbuilt functions or another widget.
In this example we’ll be using the QPushButton widget. We’ve created a submit button that calls the function
show that uses the
text() method on our
def show(): print(line.text()) line = QtWidgets.QLineEdit(win) line.move(100,80) button = QtWidgets.QPushButton(win) button.setText("Submit") button.clicked.connect(show) button.move(100,150) button = QtWidgets.QPushButton(win) button.setText("Clear") button.clicked.connect(line.clear) button.move(100,220) win.show() sys.exit(app.exec_())
We’ve also thrown in a bonus
Clear button that calls the
clear() method on the
QLineEdit widget. This deletes all the contents of the
Try the code for the PyQt5 QLineEdit widget yourself to understand more.
Other QLineEdit Methods
Here are few additional code examples that cover the most important and useful QLineEdit features.
Taking passwords from User
setEchoMode() takes several different “modes”, one of which is the password mode. This obscures the password and protects it from unwanted eyes.
def show(): print(line.text()) line = QtWidgets.QLineEdit(win) line.setEchoMode(QtWidgets.QLineEdit.Password) line.move(100,80) button = QtWidgets.QPushButton(win) button.setText("Submit") button.clicked.connect(show) button.move(100,150) win.show() sys.exit(app.exec_())
Changing widget size
setFixedWidth() method we can change the size of the
QLineEdit() widget in terms of pixels.
def show(): print(line.text()) app = QApplication(sys.argv) win = QMainWindow() win.setGeometry(400,400,300,300) win.setWindowTitle("CodersLegacy") line = QtWidgets.QLineEdit(win) line.setFixedWidth(140) line.move(80,80) win.show() sys.exit(app.exec_())
Compare the size of this one to the one shown in the start of this article. The default value of each widget is around 100 pixels.
A complete list of each method for QLineEdit widgets with a brief description.
|Decides the Alignment of the text. Takes 4 possible values, Qt.AlignLeft, Qt.AlignRight, Qt.AlignCenter and Qt.AlignJustify.|
|Deletes the contents within the |
|Controls the mode for text appearance in the widget. Values are QLineEdit.Normal, QLineEdit.NoEcho, QLineEdit.Password, QLineEdit.PasswordEchoOnEdit.|
|Defines the maximum number of characters that can be typed into the widget.|
|Makes the widget non-editable|
|The text passed to this method will appear in the widget.|
|Returns the text currently in the widget.|
|Defines the validation rules.|
|Applies mask of combination of characters for input|
|Sets the font for the widget.|
|Sets the maximum width for the Widget in pixels.|
A list of signals available for the QLineEdit widget.
|A signal that activates when the text in the QLineEdit widget is changed in any manner.|
|Almost the same as |
|This signal is emitted when the Return or Enter key is pressed or the line edit loses focus (when it’s no longer highlighted)|
|Emits a signal when the user inputs a character that’s considered “invalid”. This signal is used together with the Validator functions.|
|Emitted when either the return key or enter key is pressed by the user.|
This marks the end of the PyQt QLineEdit article. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.