You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Windows 10中将自行开发的Python PyQt5应用添加到「打开方式」菜单

实现步骤

1. 前置准备

  • 先将你的PyQt5文本编辑器打包为exe可执行文件,比如使用PyInstaller执行命令:pyinstaller -w -i your_icon.ico main.py,打包完成后将exe放到固定目录,比如C:\MyEditor\MyTextEditor.exe
  • 你的PyQt5程序需要支持接收命令行传入的文件/文件夹路径参数,参考示例代码:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow

class TextEditor(QMainWindow):
    def __init__(self, target_path=None):
        super().__init__()
        # 此处添加你的编辑器初始化逻辑
        if target_path:
            self.load_target(target_path)
    
    def load_target(self, path):
        # 此处添加你的打开文件/文件夹的业务逻辑
        pass

if __name__ == "__main__":
    app = QApplication(sys.argv)
    # 接收右键菜单传入的路径参数
    target = sys.argv[1] if len(sys.argv) > 1 else None
    editor = TextEditor(target)
    editor.show()
    sys.exit(app.exec_())

2. Python修改注册表添加右键菜单

使用Python标准库winreg操作Windows注册表即可实现需求,运行以下脚本时必须以管理员身份启动,否则会出现权限不足报错:

import winreg

# 配置项,按需修改
APP_NAME = "MyTextEditor"  # 你的程序唯一标识
APP_DISPLAY_NAME = "使用我的文本编辑器打开"  # 右键菜单显示的文字
APP_PATH = r"C:\MyEditor\MyTextEditor.exe"  # 你的exe完整路径
# 图标直接用exe本身的图标,无需额外配置
ICON_PATH = f"{APP_PATH},0"

def add_registry_entry(root, sub_path, command_param):
    """
    添加注册表项
    :param root: 注册表根节点
    :param sub_path: 子路径
    :param command_param: 命令传入的参数占位符
    """
    try:
        # 创建菜单主项
        key_path = fr"{sub_path}\{APP_NAME}"
        key = winreg.CreateKey(root, key_path)
        winreg.SetValueEx(key, "", 0, winreg.REG_SZ, APP_DISPLAY_NAME)
        winreg.SetValueEx(key, "Icon", 0, winreg.REG_SZ, ICON_PATH)
        winreg.CloseKey(key)

        # 创建command子项
        command_key_path = fr"{key_path}\command"
        command_key = winreg.CreateKey(root, command_key_path)
        command_value = f'"{APP_PATH}" "{command_param}"'
        winreg.SetValueEx(command_key, "", 0, winreg.REG_SZ, command_value)
        winreg.CloseKey(command_key)
        print(f"成功添加注册表项:{key_path}")
    except Exception as e:
        print(f"添加失败:{str(e)}")

if __name__ == "__main__":
    # 场景1:右键点击任意文件显示菜单
    add_registry_entry(winreg.HKEY_CLASSES_ROOT, r"*", "%1")
    # 场景2:右键点击文件夹显示菜单
    add_registry_entry(winreg.HKEY_CLASSES_ROOT, r"Directory", "%1")
    # 场景3:右键点击文件夹空白处显示菜单
    add_registry_entry(winreg.HKEY_CLASSES_ROOT, r"Directory\Background", "%V")
    # 额外添加到系统「打开方式」列表
    add_registry_entry(winreg.HKEY_CLASSES_ROOT, fr"Applications\{APP_NAME}.exe\shell\open", "%1")
    print("所有右键菜单配置完成")

3. 移除右键菜单的方法

如果需要删除添加的菜单,运行以下管理员权限的脚本即可:

import winreg

APP_NAME = "MyTextEditor"

def delete_registry_entry(root, sub_path):
    try:
        key_path = fr"{sub_path}\{APP_NAME}"
        # 先删除command子项
        winreg.DeleteKey(root, fr"{key_path}\command")
        # 再删除主项
        winreg.DeleteKey(root, key_path)
        print(f"成功删除注册表项:{key_path}")
    except Exception as e:
        print(f"删除失败:{str(e)}")

if __name__ == "__main__":
    delete_registry_entry(winreg.HKEY_CLASSES_ROOT, r"*")
    delete_registry_entry(winreg.HKEY_CLASSES_ROOT, r"Directory")
    delete_registry_entry(winreg.HKEY_CLASSES_ROOT, r"Directory\Background")
    # 删除打开方式列表的配置
    delete_registry_entry(winreg.HKEY_CLASSES_ROOT, fr"Applications\{APP_NAME}.exe\shell\open")
    # 再删除上层的空项
    try:
        winreg.DeleteKey(winreg.HKEY_CLASSES_ROOT, fr"Applications\{APP_NAME}.exe\shell")
        winreg.DeleteKey(winreg.HKEY_CLASSES_ROOT, fr"Applications\{APP_NAME}.exe")
    except:
        pass
    print("所有右键菜单配置已移除")

注意事项

  • 如果不需要所有用户都能使用该菜单,可以将代码中的winreg.HKEY_CLASSES_ROOT替换为winreg.HKEY_CURRENT_USER,运行时无需管理员权限,仅当前登录用户可见该右键选项
  • 不要随意移动exe的位置,移动后需要重新运行添加脚本更新注册表中的路径
  • 路径尽量使用纯英文,避免中文或特殊字符导致路径识别失败

内容的提问来源于stack exchange,提问作者Jr.Abdul Wahab Melethil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 21:45:03