The focusWidget() method of the QApplication class returns a reference to the object that is in focus. When you start the program in an object ( QLineEdit ), the cursor flashes and you can enter data, that is, as far as I understand, the object is in focus. Why then app.focusWidget() returns None ?

 import sys from PyQt5.QtWidgets import (QWidget, QLineEdit, QApplication) class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): qle = QLineEdit(self) qle.move(60, 100) print("app.focusWidget(): ", app.focusWidget()) self.setGeometry(300, 300, 280, 170) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_()) 

    1 answer 1

    It returns None because at the time of the print(... operation print(... there are no widgets on the screen yet. They will appear only after ex.show() , while the app.exec_()

    To print which widget is in focus during operation, you can start a timer, for example, by adding in the constructor or initUI:

     timer = QTimer(self); timer.timeout.connect(lambda: print("app.focusWidget(): ", app.focusWidget())) timer.start(1000); 
    • can be easier: QTimer.singleShot(1000, lambda: print("app.focusWidget(): ", app.focusWidget())) - gil9red