简体   繁体   中英

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

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,

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM