qwerty
qwerty

Reputation: 229

PyQt application and infinite loop

def main():
    app = QtGui.QApplication(sys.argv)

    gui = GUIClass()
    gui.showUI()

    app.exec_()

    while True:
        if win32api.GetAsyncKeyState(win32con.VK_SHIFT):
            print(True)

if __name__ == '__main__':
    main()

Code after app.exec_() doesn't running. How to do infinite loop and run my PyQt application?

Thanks.

Upvotes: 0

Views: 3486

Answers (1)

mdurant
mdurant

Reputation: 28683

pyqt comes with its own (infinite) event loop so that you don't have to build your own. app.exec_() enters this loop, which is why you don't see the code following that execute. Only after you have closed all qt windows will anything remaining be executed.

Example of QTimer usage:

in main() before exec_():

def timout():
    if win32api.GetAsyncKeyState(win32con.VK_SHIFT):
        print(True)

timer = QtCore.QTimer(self)
timer.timeout.connect(timeout)
timer.start(100)

Upvotes: 4

Related Questions