简体   繁体   English

尝试为 python 截图制作一个计时器

[英]trying to make a timer for python screenshots

I am trying to make a script that will take a screenshot every 30 seconds.我正在尝试制作一个脚本,每 30 秒截取一次屏幕截图。

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.如果你想在一段时间后打破它,这个永远运行的简单 while 代码将起作用,你可以使用索引变量代替。

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.您当然可以用您的代码替换 g() 。 If you do not set the thread to daemon , the program will not quit, because the thread function f() runs infinitely.如果不设置线程为daemon ,程序不会退出,因为线程function f()会无限运行。 If it is set to daemon, the thread will exit the moment the main code exits.如果设置为daemon,线程会在主代码退出的瞬间退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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