如何使函数在特定时间启动

2024-04-24 06:28:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个程序,以便在特定时间自动进入一个站点(我知道这是更好的方法) 但这是一个问题,程序没有等到正确的时间,它只是在按下按钮时自动启动。我试着像闹钟一样去做,很多闹钟都是一样的。我是如何做这项工作的?谢谢下面是我正在做的一个简单示例:

import pynput
from datetime import *
import tkinter
from tkinter import *
import pyautogui
import time
from pynput.keyboard import Key, Controller
win = Tk()
win.title('autoclass.enter V1')
win.geometry('500x300')

hora= Entry(win, width=30)
hora.place(x=178, y=100)
hour = hora.get()

def start():

    while(1 == 1):
        won = str(datetime.now().time())
        now = (won[11:10])
        print(now)
        if (hour == now):
            print(now)
        
            stop = True
        
            pyautogui.click(a, b)
            time.sleep(g)
            pyautogui.moveTo(c, d, duration = 1)#I had change the numbers by letters to not destroi ur dektop
            pyautogui.click(e, f)
            keyboard = Controller()
            time.sleep(2)
            time.sleep(8)
            keyboard.type(hello)
            keyboard.press(Key.enter)
            keyboard.release(Key.enter)
            break

myLabel6 = Label(win, text='the hour(like this 7:30')
myLabel6.place(x=195, y=78)

button = Button(win, text='start', command=start)
button.place(x=50, y=80)
win.mainloop()

Tags: keyfromimport程序timeplacesleepstart
1条回答
网友
1楼 · 发布于 2024-04-24 06:28:20

您可以使用time模块和while循环检查当前时间是否等于所需的时间

from time import gmtime, strftime
desired_time = '13:28'
while True:
    if strftime("%H:%M", gmtime()) == desired_time:
        main()

但在特金特while loop将冻结该程序。我建议创建一个函数,根据if语句检查时间

def time_checker():
    if strftime("%H:%M", gmtime()) == desired_time:
        main()

相关问题 更多 >