Python写开机启动管理工具:添加与删除功能,管理开机启动程序

admin 2024年2月23日19:01:46评论15 views字数 4081阅读13分36秒阅读模式

Python写开机启动管理工具:添加与删除功能,管理开机启动程序

使用方法:

  1. 双击运行

  2. 添加开机启动项:将需要开机启动的程序文件拖拽到窗口中,程序会自动将其添加为开机启动项。

  3. 刷新启动项列表:点击窗口中的"刷新一下"按钮,可以重新加载当前的启动项列表

  4. 删除启动项:选择要删除的启动项,然后点击窗口中的"删除启动项"按钮,可以删除选中的启动项。

代码:

import sysimport timeimport winregimport tkinter as tkfrom tkinter import ttkimport windndimport ctypesimport osstartup_items = []def modify_startup_entry(path):    exe_name = os.path.splitext(os.path.basename(path))[0]    # 打开注册表键    reg_path = r"SoftwareMicrosoftWindowsCurrentVersionRun"    with winreg.OpenKey(winreg.HKEY_CURRENT_USER, reg_path, 0, winreg.KEY_ALL_ACCESS) as key:        # 设置启动项        winreg.SetValueEx(key, exe_name, 0, winreg.REG_SZ, path)def handle_drag(files):    if files:        # 解码文件路径        path = files[0].decode('gbk')        # 检查启动项是否已存在        startup_items = get_startup_items()        exe_name = os.path.splitext(os.path.basename(path))[0]        for name, _ in startup_items:            if name == exe_name:                ctypes.windll.user32.MessageBoxW(0, "启动项已存在,请更改程序名称进行添加启动项。", "提示", 0x40)                return        # 修改启动项        modify_startup_entry(path)        # 弹出成功提示        ctypes.windll.user32.MessageBoxW(0, "启动项添加成功!", "提示", 0x40)        # 刷新启动项列表        refresh_startup_items()    else:        # 弹出错误提示        ctypes.windll.user32.MessageBoxW(0, "没有拖拽文件或路径无效", "提示", 0x40)def get_startup_items():    startup_items = []    reg_path = r"SoftwareMicrosoftWindowsCurrentVersionRun"    with winreg.OpenKey(winreg.HKEY_CURRENT_USER, reg_path, 0, winreg.KEY_READ) as key:        try:            index = 0            while True:                name, value, _ = winreg.EnumValue(key, index)                startup_items.append((name, value))                index += 1        except OSError:            pass    return startup_itemsdef delete_startup_item(item_name):    reg_path = r"SoftwareMicrosoftWindowsCurrentVersionRun"    with winreg.OpenKey(winreg.HKEY_CURRENT_USER, reg_path, 0, winreg.KEY_ALL_ACCESS) as key:        try:            winreg.DeleteValue(key, item_name)        except Exception as e:            ctypes.windll.user32.MessageBoxW(0, f"删除启动项失败:{str(e)}", "错误", 0x40)def delete_selected_items():    selected_indices = listbox.curselection()    if selected_indices:        # 获取选择的启动项数量        num_selected = len(selected_indices)        # 弹出确认删除的提示框        result = ctypes.windll.user32.MessageBoxW(0, f"确定要删除选中的 {num_selected} 个启动项吗?", "确认删除", 0x4 | 0x20)        if result == 6:  # 用户点击了"是"按钮            for index in selected_indices:                item_name = startup_items[index][0]                delete_startup_item(item_name)            # 刷新启动项列表            refresh_startup_items()            ctypes.windll.user32.MessageBoxW(0, "启动项删除成功!", "提示", 0x40)    else:        ctypes.windll.user32.MessageBoxW(0, "请先选择要删除的启动项。", "提示", 0x40)def refresh_startup_items():    global startup_items    startup_items = get_startup_items()    if startup_items:        listbox.delete(0, tk.END)        for name, value in startup_items:            listbox.insert(tk.END, f'{name}: {value}')    else:        ctypes.windll.user32.MessageBoxW(0, "未找到启动项。", "信息", 0x40)if __name__ == "__main__":    root = tk.Tk()    root.title("开机启动项管理工具--微信公众号:蓝胖子之家")    root.geometry("700x350")  # 设置窗口大小    root.resizable(False, False)    status_label = tk.Label(root, text="请将需要开机启动的程序拖拽进来添加开机启动...", font=("Arial", 14, "bold"))    status_label.pack(pady=10)    # 注册拖拽事件处理函数    windnd.hook_dropfiles(root, func=handle_drag)    button_frame = tk.Frame(root)    button_frame.pack(pady=10)    refresh_button = tk.Button(button_frame, text='刷新一下', command=refresh_startup_items, width=20, height=2, bg='lightblue', fg='white')    refresh_button.pack(side=tk.LEFT, padx=5)    delete_button = tk.Button(button_frame, text='删除启动项', command=delete_selected_items, width=20, height=2, bg='red', fg='white')    delete_button.pack(side=tk.LEFT, padx=5)    listbox = tk.Listbox(root, width=80, height=10, selectmode=tk.MULTIPLE)  # 调整Listbox的宽度和高度    listbox.pack(pady=10)    startup_items = get_startup_items()    for name, value in startup_items:        listbox.insert(tk.END, f'{name}: {value}')    root.mainloop()
  1. 导入了必要的模块和库,如sys、time、winreg、tkinter等。

  2. 定义了一个空的启动项列表startup_items

  3. 定义了修改启动项的函数modify_startup_entry,用于将指定的程序路径添加到注册表中作为启动项。

  4. 定义了处理拖拽事件的函数handle_drag,用于处理拖拽文件的操作。如果拖拽了文件,会检查启动项是否已存在,如果不存在则调用modify_startup_entry函数添加启动项,并弹出成功提示;如果已存在则弹出错误提示。

  5. 定义了获取启动项列表的函数get_startup_items,通过读取注册表中的启动项信息,返回一个包含启动项名称和值的列表。

  6. 定义了删除启动项的函数delete_startup_item,用于删除指定名称的启动项。

  7. 定义了删除选中启动项的函数delete_selected_items,首先获取用户选择的启动项索引,然后弹出确认删除的提示框,如果用户确认删除,则依次调用delete_startup_item函数删除选中的启动项,并刷新启动项列表。

  8. 定义了刷新启动项列表的函数refresh_startup_items,通过调用get_startup_items函数获取最新的启动项列表,并更新显示在GUI界面的Listbox中。

  9. 在主程序入口处,创建了一个Tkinter窗口root,并设置了窗口标题、大小和不可调整大小的属性。

  10. 创建了一个标签控件status_label,用于显示提示信息。

  11. 注册了拖拽事件处理函数handle_drag

  12. 创建了按钮控件refresh_buttondelete_button,并绑定了对应的刷新和删除函数。

  13. 创建了一个Listbox控件listbox,用于显示启动项列表。

  14. 获取并显示当前的启动项列表。

  15. 进入Tkinter的事件循环,等待用户操作

运行效果:

Python写开机启动管理工具:添加与删除功能,管理开机启动程序

Python写开机启动管理工具:添加与删除功能,管理开机启动程序

后台回复启动项管理获取文章工具

原文始发于微信公众号(蓝胖子之家):Python写开机启动管理工具:添加与删除功能,管理开机启动程序

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2024年2月23日19:01:46
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   Python写开机启动管理工具:添加与删除功能,管理开机启动程序http://cn-sec.com/archives/2519623.html

发表评论

匿名网友 填写信息