In this tutorial we’ll make an app that displays an input field.

A single line text field is one of the first widgets you will reach for. The class is QLineEdit. The user types into it and your code reads what they typed with text().

A single line text field is one of the first widgets you will use. QLineEdit is the class for it. The user types into it and your code reads the text with text().

A textbox or LineEdit can be created using the QLineEdit class. Many applications have text inputs like form fields and the like.

QLineEdit

PyQt line edit

Start by importing the QLineEdit widget:

from PyQt5.QtWidgets import QLineEdit

We will also add a text label, to show the user what to type.
Import QLabel:

from PyQt5.QtWidgets import QLabel

Then add both to the screen:

self.nameLabel = QLabel(self)
self.nameLabel.setText('Name:')
self.line = QLineEdit(self)

self.line.move(80, 20)
self.line.resize(200, 32)
self.nameLabel.move(20, 20)

The text value can be printed using:

print('Your name: ' + self.line.text())

QLineEdit Example

We start with the code followed by the explanation.

import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QWidget, QLabel, QLineEdit
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import QSize

class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)

self.setMinimumSize(QSize(320, 140))
self.setWindowTitle("PyQt Line Edit example (textfield) - pythonprogramminglanguage.com")

self.nameLabel = QLabel(self)
self.nameLabel.setText('Name:')
self.line = QLineEdit(self)

self.line.move(80, 20)
self.line.resize(200, 32)
self.nameLabel.move(20, 20)

pybutton = QPushButton('OK', self)
pybutton.clicked.connect(self.clickMethod)
pybutton.resize(200,32)
pybutton.move(80, 60)

def clickMethod(self):
print('Your name: ' + self.line.text())

if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit( app.exec_() )

Useful things to set

  • setPlaceholderText('your name') shows grey text while the field is empty.
  • setText('hello') fills the field with a starting value.
  • setMaxLength(20) limits how much the user can type.
  • setValidator(QIntValidator(0, 100)) only accepts whole numbers in a range.
  • textChanged fires on every keystroke, returnPressed fires when the user presses enter.
  • setReadOnly(True) shows text without letting the user change it.

A password field is the same widget with setEchoMode(QLineEdit.Password).

Useful things to set

  • setPlaceholderText('your name') shows grey text while the field is empty, so the user knows what to type.
  • setText('hello') fills the field with a starting value.
  • setMaxLength(20) limits how much can be typed.
  • setValidator(QIntValidator(0, 100)) only accepts whole numbers in a range.
  • textChanged fires on every keystroke, returnPressed only when the user presses enter.
  • setReadOnly(True) shows text without letting the user change it.
  • clear() empties the field.

A password field is the same widget with setEchoMode(QLineEdit.Password), which hides what is typed.

Press enter to submit is nicer than a button, so connect returnPressed to the same function as your button.

The fastest way to learn this is to write it yourself. The PyChallenge exercises run in your browser, no install needed.