Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I tried to call the function using delay time and taking the help of QTimer.singleShot function but is not getting any output.

from PyQt5.QtCore import QTimer

list1=[1,2,3,4,5]

delay = 2500

def calling_func():

    if list1:

        list_item = list1.pop()

        QTimer.singleShot(delay, lambda: target_func(list_item))

def target_func(list_item):

    print("fid= ",list_item)

    QTimer.singleShot(delay, calling_func)

calling_func()

I am expecting to output the list_item value one by one in the target_func but my function is not being called.

1 Answer

0 votes
by (36.8k points)

The asynchronous elements of Qt such as signals and timers use the eventloop for their execution, in your case there are none so it fails. The solution is to create a Q{Core, Gui} Application:

from PyQt5.QtCore import QCoreApplication, QTimer

list1=[1,2,3,4,5]

delay = 2500

def calling_func():

    if list1:

        list_item = list1.pop()

        QTimer.singleShot(delay, lambda: target_func(list_item))

def target_func(list_item):

    print("fid= ",list_item)

    QTimer.singleShot(delay, calling_func)

app = QCoreApplication([])

calling_func()

app.exec_()

If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch

Browse Categories

...