Suppose I enter text into a text field, press Enter and the text from the text field falls into a label (Label). The process itself needs to understand how keystrokes are handled.

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

The operating system constantly monitors the state of the peripheral devices and sends messages to a program about a particular event, depending on different conditions. Further, these signals can be processed within the program.

The PyQt program hides from you the entire complex processing process. As a result, you just need to know what widgets these events receive. You can also ask the widget to perform any of your function, if it received an event.

In PyQt, this is called a signal and slot system.

For example, here are the signals (events) that QLineEdit can receive . Among them is the signal returnPressed() . It only remains to add our function to the slot, so that PyQt would call it when it received an event of pressing Enter and set the desired text to QLabel.

 #!/usr/bin/python3 # -*- coding: utf-8 -*- import sys from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QMessageBox, QLabel class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.resize(250, 150) self.move(300, 300) self.setWindowTitle('Simple') self.le = QLineEdit(self) self.le.move(22, 22) self.le.returnPressed.connect(self.pressedKeys) self.lbl = QLabel(self) self.lbl.move(22, 40) self.lbl.resize(100,20) self.show() def pressedKeys(self): print(self.le.text()) self.lbl.setText(self.le.text()) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())