Home  >  Article  >  Backend Development  >  Detailed explanation of the steps to add an opening method in the right-click menu of an application using Python

Detailed explanation of the steps to add an opening method in the right-click menu of an application using Python

高洛峰
高洛峰Original
2017-03-27 16:35:232016browse

Recently, a small tool developed by the project team wanted to add an opening method in the right-click menu. Taking Youdao Cloud Notes as an example, the requirements were disassembled and the code was written

1. Requirements dismantling :

How to manually add a right-click menu to open:

Step1: Open the registry editor, Win +R->Enter "regedit"

Detailed explanation of the steps to add an opening method in the right-click menu of an application using Python

##Step2: In HKEY_CLASS ES_ROOT/*/shell (or HKEY_LOCAL_MACHINE/SOFTWARE/Classes/*/shell, the two directories are the same) Add a key: YNote, then create a new item command in this item, and then edit String , add the path of the application, and finally add a space and "%1" after the path and name, and then right-click to find the way to open YNote

Detailed explanation of the steps to add an opening method in the right-click menu of an application using Python

2. Code implementation

Method1: implemented through _winreg module:

import _winreg
from _winreg import KEY_ALL_ACCESS 

with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Classes\*\shell") as key:
    print key

    newKey = _winreg.CreateKeyEx(key,"YNote",0,KEY_ALL_ACCESS)
    
    sub_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,r"SOFTWARE\Classes\*\shell\YNote")
    newsubKey = _winreg.CreateKey(sub_key,"command")
    
    _winreg.SetValue(newsubKey,"(Default)",1,"\"C:\Program Files (x86)\Youdao\YoudaoNote\YoudaoNote.exe\" \"%1\"")

Method2: passed win32api and win32con module implementation

import win32api
import win32con

key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE,r"SOFTWARE\Classes\*\shell")
    
newKey = win32api.RegCreateKey(key,"YNote")
    
sub_key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE,r"SOFTWARE\Classes\*\shell\YNote")

newsubKey = win32api.RegCreateKey(sub_key,"command")
    
win32api.RegSetValue(newsubKey,"(Default)", win32con.REG_SZ,"\"C:\Program Files (x86)\Youdao\YoudaoNote\YoudaoNote.exe\" \"%1\"")


The above is the detailed content of Detailed explanation of the steps to add an opening method in the right-click menu of an application using Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn