首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何让函数在某一时刻启动

如何让函数在某一时刻启动
EN

Stack Overflow用户
提问于 2020-07-22 02:40:21
回答 1查看 53关注 0票数 0

我正在做一个程序,在某个时间自动进入一个网站(我知道这是其他的方法),但这是一个问题,程序没有等到正确的时间,当按下按钮时,它就会自动启动。我试着这样做,像一个警报,许多警报形式,但它总是相同的。我是怎么做这项工作的?谢谢。下面是我正在做的一个简单的例子:

代码语言:javascript
运行
复制
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()
EN

Stack Overflow用户

发布于 2020-07-22 02:41:17

您可以使用time模块和while循环来检查当前时间是否等于您想要的时间。

代码语言:javascript
运行
复制
from time import gmtime, strftime
desired_time = '13:28'
while True:
    if strftime("%H:%M", gmtime()) == desired_time:
        main()

但在Tkinter中,while loop将冻结程序。我建议做一个根据那个if语句检查时间的函数

代码语言:javascript
运行
复制
def time_checker():
    if strftime("%H:%M", gmtime()) == desired_time:
        main()
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63021062

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档