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.
Related course:
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 *
app = QApplication(sys.argv) w = QWidget() w.setWindowTitle('Textbox example @pythonspot.com')
textbox = QLineEdit(w) textbox.move(20, 20) textbox.resize(280,40)
w.resize(320, 150)
button = QPushButton('Click me', w) button.move(20,80)
@pyqtSlot() def on_click(): textbox.setText("Button clicked.")
button.clicked.connect(on_click)
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:
button.clicked.connect(on_click)
|
This function sets the textbox using setText().
Download PyQT Code (Bulk Collection)
Leave a Reply: