In this PyQt5 article i want to show you PyQt5 Creating QLineEdit With returnPressed Signal , also iam going to show you how you can connect returnPressed signal of QLineEdit with Slot. a line edit allows the user to enter and edit a single line of plain text with a useful collection of editing functions, including undo and redo, cut and paste, and drag and drop (see setDragEnabled()). By changing the echoMode() of a line edit, it can also be used as a “write-only” field, for inputs such as passwords.
Join PyQt5 Full Course for Free
PyQt5 Course
Also you can read more Python GUI articles in the below links
- Kivy GUI Development Tutorials
- TKinter GUI Development Tutorials
- Pyside2 GUI Development
- wxPython GUI Development Tutorials
- PyQt5 GUI Development Tutorials
First we need some imports.
1 2 3 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QLineEdit, QLabel import sys |
After that we are going to create our main Window class that extends from QWidget. and in the constructor of the class we need to add some requirements of the window like set window title, window icon and window geometry.
1 2 3 4 5 6 7 8 9 10 11 |
class Window(QWidget): def __init__(self): super().__init__() self.title = "PyQt5 Window" self.top = 200 self.left = 500 self.width = 400 self.height = 300 self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) |
In here we are going to create a QHBoxLayout object with QLineEdit, also we have set font size and font style for the lineedit.
1 2 3 |
hbox = QHBoxLayout() self.lineedit = QLineEdit(self) self.lineedit.setFont(QtGui.QFont("Sanserif", 15)) |
We have connected the returnPressed signal of QLineEdit with the onPressed() method that we will create.
1 |
self.lineedit.returnPressed.connect(self.onPressed) |
In here we have created a QLabel, also we have set the font for the label.
1 2 |
self.label = QLabel(self) self.label.setFont(QtGui.QFont("Sanserif", 15)) |
We need to add our label and lineedit in the hbox layout, also we need to set the layout for our main window. if you don’t do this you will not see any widget in the window.
1 2 3 |
hbox.addWidget(self.label) hbox.addWidget(self.lineedit) self.setLayout(hbox) |
Also we need to show the window.
1 |
self.show() |
Now we create our onPressed() method that we have already connected this with the signal of QLineEdit. basically in this method we just get the text from the line edit and we set that to the label.
1 2 |
def onPressed(self): self.label.setText(self.lineedit.text()) |
Also every PyQt5 application must create an application object.
1 |
App = QApplication(sys.argv) |
Finally, we enter the mainloop of the application. The event handling starts from this point.
1 2 |
window = Window() sys.exit(App.exec()) |
Complete source code PyQt5 Creating QLineEdit With returnPressed Signal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QLineEdit, QLabel import sys class Window(QWidget): def __init__(self): super().__init__() self.title = "PyQt5 Window" self.top = 200 self.left = 500 self.width = 400 self.height = 300 self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) hbox = QHBoxLayout() self.lineedit = QLineEdit(self) self.lineedit.setFont(QtGui.QFont("Sanserif", 15)) self.lineedit.returnPressed.connect(self.onPressed) self.label = QLabel(self) self.label.setFont(QtGui.QFont("Sanserif", 15)) hbox.addWidget(self.label) hbox.addWidget(self.lineedit) self.setLayout(hbox) self.show() def onPressed(self): self.label.setText(self.lineedit.text()) if __name__ == "__main__": App = QApplication(sys.argv) window = Window() sys.exit(App.exec_()) |
Run the complete code and this will be the result.
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email