PyQt4 textbox


pyqt textbox
PyQt4 textbox example
In this article you will learn how to interact with a textbox using PyQt4.

If you want to display text in a textbox (QLineEdit) you could use the setText() method.

Download PyQT Code (Bulk Collection)

PyQt4 QLineEdit

The textbox example below changes the text if the button is pressed.

import sys
from PyQt4.QtCore import pyqtSlot
from PyQt4.QtGui import *
 
# create our window
app = QApplication(sys.argv)
w = QWidget()
w.setWindowTitle('Textbox example @pythonspot.com')
 
# Create textbox
textbox = QLineEdit(w)
textbox.move(20, 20)
textbox.resize(280,40)
 
# Set window size.
w.resize(320, 150)
 
# Create a button in the window
button = QPushButton('Click me', w)
button.move(20,80)
 
# Create the actions
@pyqtSlot()
def on_click():
    textbox.setText("Button clicked.")
 
# connect the signals to the slots
button.clicked.connect(on_click)
 
# Show the window and run the app
w.show()
app.exec_()

The text field is created with the lines:

textbox = QLineEdit(w)
textbox.move(20, 20)
textbox.resize(280,40)

The button (from screenshot) is made with:

button = QPushButton('Click me', w)

We connect the button to the on_click function by:

# connect the signals to the slots
button.clicked.connect(on_click)

This function sets the textbox using setText().