Replies: 1 comment
-
Hello, Could you please share the details of the problem in English? It's not clear how the code snippet is related to Qodana |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
import tkinter as tk
from tkinter import messagebox
import win32com.client
import logging
设置记录
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def list_tasks():
try:
scheduler = win32com.client.Dispatch("Schedule.Service")
scheduler.Connect()
root_folder = scheduler.GetFolder("\")
tasks = root_folder.GetTasks(0)
listbox.delete(0, tk.END)
for task in tasks:
listbox.insert(tk.END, task.Name)
except Exception as e:
logging.error(f"Failed to list tasks: {e}")
messagebox.showerror("错误", "无法列出任务")
def run_task(task_name):
try:
scheduler = win32com.client.Dispatch("Schedule.Service")
scheduler.Connect()
root_folder = scheduler.GetFolder("\")
task = root_folder.GetTask(task_name)
if task.Definition.Settings.Enabled:
task.Run()
logging.info(f"Task {task_name} started.")
messagebox.showinfo("任务执行", f"任务 {task_name} 已启动")
else:
logging.info(f"Task {task_name} is disabled, cannot start.")
messagebox.showinfo("任务状态", f"任务 {task_name} 已禁用,无法启动")
except Exception as e:
logging.error(f"Failed to run task {task_name}: {e}")
messagebox.showerror("错误", "无法启动任务")
def disable_task(task_name):
try:
scheduler = win32com.client.Dispatch("Schedule.Service")
scheduler.Connect()
root_folder = scheduler.GetFolder("\")
task = root_folder.GetTask(task_name)
if task.Enabled:
task.Enabled = False
task.RegisterChanges(6) # 6 means TASK_CHANGE_ENABLE
logging.info(f"Task {task_name} disabled.")
messagebox.showinfo("任务操作", f"任务 {task_name} 已禁用")
else:
logging.info(f"Task {task_name} is already disabled.")
messagebox.showinfo("任务状态", f"任务 {task_name} 已禁用")
except Exception as e:
logging.error(f"Failed to disable task {task_name}: {e}")
messagebox.showerror("错误", "无法禁用任务")
def disable_all_tasks():
try:
scheduler = win32com.client.Dispatch("Schedule.Service")
scheduler.Connect()
root_folder = scheduler.GetFolder("\")
tasks = root_folder.GetTasks(0)
for task in tasks:
if task.Enabled:
task.Enabled = False
task.RegisterChanges(6) # 6 means TASK_CHANGE_ENABLE
logging.info(f"Task {task.Name} disabled.")
messagebox.showinfo("任务操作", "所有任务已禁用")
except Exception as e:
logging.error(f"Failed to disable all tasks: {e}")
messagebox.showerror("错误", "无法禁用所有任务")
app = tk.Tk()
app.title("Windows任务计划程序")
listbox = tk.Listbox(app, height=20, width=60)
listbox.pack(pady=20)
btn_list = tk.Button(app, text="查看计划任务", command=list_tasks)
btn_list.pack(side=tk.LEFT, padx=(20, 10))
btn_run = tk.Button(app, text="开启计划任务", command=lambda: run_task(listbox.get(listbox.curselection())))
btn_run.pack(side=tk.LEFT, padx=(10, 10))
btn_disable = tk.Button(app, text="停止计划任务", command=lambda: disable_task(listbox.get(listbox.curselection())))
btn_disable.pack(side=tk.LEFT, padx=(10, 10))
btn_disable_all = tk.Button(app, text="停止所有计划任务", command=disable_all_tasks)
btn_disable_all.pack(side=tk.LEFT, padx=(10, 10))
app.mainloop()
代码运行后只能查看计划任务 开启计划任务就显示无法启动任务
Beta Was this translation helpful? Give feedback.
All reactions