'trying to make a timer for python screenshots

I am trying to make a script that will take a screenshot every 30 seconds.

This is what I have now:

def on_click(x, y, button, pressed):
    global path
    if pressed:
        takeScreenshoot(path)
        print('ScreenShoot Taken')

What I have tried to do.

import time
while True: # Change for a variable or a toggle
       time.sleep(30)
       takeScreenshoot(path)
       print('ScreenShoot Taken')

however now because of this the next part of my code is unreache able



Solution 1:[1]

import time
while True: # Change for a variable or a toggle
       time.sleep(30)
       takeScreenshoot(path)
       print('ScreenShoot Taken')

This might be something you could do,

Solution 2:[2]

Well it is simple

import time
while True:
    takeScreenshot(path)
    time.sleep(30)
    print('screenshot taken')

This simple while code that runs forever will work if you want to break it after sometime you can use an index variable instead.

Solution 3:[3]

from threading import Thread
import time

def f(x):
   while True:
    time.sleep(2.1)
    print(f'ScreenShoot Taken: {x}')


def g(x):
   for i in range(5):
    time.sleep(2.2)
    print(f'Ho Ho Ho: {x}')


f = Thread(target=f, args=["bla"])
f.daemon = True
f.start()
g("bla")

In this example both functions work at the same time. You can of course replace g() with your code. If you do not set the thread to daemon, the program will not quit, because the thread function f() runs infinitely. If it is set to daemon, the thread will exit the moment the main code exits.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thornily
Solution 2 ROOP AMBER
Solution 3 Thomas