我尝试在系统使用 Microsoft 任务计划程序启动时自动运行 python 脚本,但我不断收到此错误

问题描述 投票:0回答:1
NameError: name 'btnontart' is not defined
Exception in thread Thread-2 (schedullle):
Traceback (most recent call last):
  File "C:\Users\Nwachukwu Sarah\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "C:\Users\Nwachukwu Sarah\AppData\Local\Programs\Python\Python310\lib\threading.py", line 946, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Nwachukwu Sarah\eyecare.py", line 78, in schedullle
    root_folder.RegisterTaskDefinition('Test Task',task_def,TASK_CREATE_OR_UPDATE,'',  '',TASK_LOGON_NONE)
  File "<COMObject GetFolder>", line 3, in RegisterTaskDefinition
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147024891), None)

这是我在单击 tkinter 按钮后收到的错误,该按钮应该调用将安排任务的函数。 我尝试创建一个每两分钟运行一次的任务,并且它有效。但是应该在系统启动时运行的程序返回错误。 请帮忙,很紧急。 这是我的代码如下

def schedullle():
    import datetime
    import win32com.client

    scheduler = win32com.client.Dispatch('Schedule.Service')
    scheduler.Connect()
    root_folder = scheduler.GetFolder('\\')
    task_def = scheduler.NewTask(0)

    # Defining the Start time of job
    start_time = datetime.datetime.now()# + datetime.timedelta(minutes=1)
    end_time = datetime.datetime.now() + datetime.timedelta(days=1080000)

    # For Daily Trigger set this variable to 2 ; for One time run set this value as 1
    TASK_TRIGGER_BOOT=8
    trigger = task_def.Triggers.Create(TASK_TRIGGER_BOOT)
    #startTime = "2023-09-15T03:49:02"
    #endTime = "2006-05-02T10:52:02"
    #trigger.StartBoundary = startTime
    
    

    #Repeat for a duration of number of day
    #num_of_days = 10
    #trigger.Repetition.Duration = "P"+str(num_of_days)+"D"

    #use PT2M for every 2 minutes, use PT1H for every 1 hour
    #trigger.Repetition.Interval = "PT2M"
    trigger.StartBoundary = start_time.isoformat()
    trigger.EndBoundary = end_time.isoformat()
    
    trigger.ExecutionTimeLimit = "PT5M"
    trigger.Id = "BootTriggerId"
    trigger.Delay = "PT30S"  
    # Create action
    TASK_ACTION_EXEC = 0
    action = task_def.Actions.Create(TASK_ACTION_EXEC)
    action.ID = 'TRIGGER BATCH'
    action.Path = "C:\\Users\\Nwachukwu Sarah\\test.bat"
    #action.Arguments ='/c start "" "C:\\Users\\Nwachukwu Sarah\\test.bat"'

    # Set parameters
    task_def.RegistrationInfo.Description = 'Test Task'
    task_def.RegistrationInfo.Author='Sarah'
    task_def.Settings.Enabled = True
    task_def.Settings.StopIfGoingOnBatteries = False
    task_def.Settings.StartWhenAvailable = True

    # Register task
    # If task already exists, it will be updated
    TASK_CREATE_OR_UPDATE = 6
    TASK_LOGON_NONE = 0
    root_folder.RegisterTaskDefinition('Test Task',task_def,TASK_CREATE_OR_UPDATE,'',  '',TASK_LOGON_NONE)

您能帮忙编写一个有效的代码吗? 我不确定,但我认为问题出在触发器上,因为日常触发器可以工作,但计算机启动时的触发器不起作用。

python windows taskscheduler
1个回答
0
投票

重置按钮的名称我认为您在使用按钮名称 btnonstart 时犯了错误,可能是按钮,但您正在使用 btnontart

© www.soinside.com 2019 - 2024. All rights reserved.