PyQt5 textbox example

In this article you will learn how to use a textbox in PyQt5. The widget is called QLineEdit and has the methods setText() to set the textbox value and text() to get the value.

We can set the size of the textbox using the resize(width,height) method. The position can be set using the move(x,y) method or using a grid layout.

Related course:
Create Simple GUI Applications with Python and Qt

PyQt5 textbox
Creation of the textbox is fairly straightforward:

self.textbox = QLineEdit(self)
self.textbox.move(20, 20)


PyQt5 textbox example

The example below creates a window with a textbox.

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QPushButton, QAction, QLineEdit, QMessageBox
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class App(QMainWindow):
    def __init__(self):
        self.title = 'PyQt5 textbox -'
        self.left = 10 = 10
        self.width = 400
        self.height = 140
    def initUI(self):
        self.setGeometry(self.left,, self.width, self.height)
        # Create textbox
        self.textbox = QLineEdit(self)
        self.textbox.move(20, 20)
        # Create a button in the window
        self.button = QPushButton('Show text', self)
        # connect button to function on_click
    def on_click(self):
        textboxValue = self.textbox.text()
        QMessageBox.question(self, 'Message -', "You typed: " + textboxValue, QMessageBox.Ok, QMessageBox.Ok)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()

Download PyQT Code (Bulk Collection)

2 thoughts on “PyQt5 textbox example

  1. Vijaya Bhaskar - March 17, 2018

    Hi Frank,
    what is the use of this method @pyqtSlot()? what does @ depict ?

    1. Frank
      Frank - March 18, 2018

      Its a PyQt slot, this is how we can link signals.