一种简单直观的方法,将您的自定义脚本添加到窗口右键单击ContextMenu。
您可以从此存储库(https://raw.githubusercontent.com/naveennamani/naveennamani/pywin_contextmenu/master/master/pywin_contextmenu.py)下载pywin_contextmenu.py文件。
或者,您可以使用PYPI安装此软件包
pip install pywin-contextmenu只是在您的脚本中导入。
import pywin_contextmenu as pycm import pywin_contextmenu as pycm
# create a group
group1 = pycm . ContextMenuGroup ( "My python scripts" )
# convert your script into executable command
script_cmd = pycm . python_script_cmd ( "scripts/clean_empty_folders.py" , rel_path = True , hide_terminal = True )
# create the item
item1 = pycm . ContextMenuItem ( "Clean empty folders" , script_cmd )
# add item to the group
group1 . add_item ( item1 )
# or
group1 . add_items ([ item1 ])
# get root_key
rk = pycm . get_root ( pycm . UserType . CURR_USER , pycm . RootType . DIR_BG )
# Create the group and test
group1 . create ( rk )
########################
# In a more pythonic way
########################
pycm . ContextMenuGroup ( "Group 1" , items = [
pycm . ContextMenuItem ( "Open CMD" , "cmd.exe" ),
pycm . ContextMenuItem ( "Open CMD 2" , "cmd.exe" )
]). create (
pycm . get_root ( pycm . UserType . CURR_USER , pycm . RootTYpe . DIR_BG )
) import pywin_contextmenu as pycm
def test_function ( file_or_dir_name ):
print ( file_or_dir_name )
input ( "Press ENTER to continue" )
# create the nested groups to execute direct commands, python functions
# and python scripts
cmgroup = pycm . ContextMenuGroup ( "Group 1" , items = [
pycm . ContextMenuItem ( "Open cmd" , "cmd.exe" ),
pycm . PythonContextMenuItem ( "Test py" , test_function ),
pycm . ContextMenuGroup ( "Group 2" , items = [
pycm . PythonContextMenuItem ( "Python script test" , pycm . python_script_cmd (
"example.py" , rel_path = True , hide_terminal = True
))
])
])
# create the group for the current user to be shown on right click of
# directory, directory background and for python files
cmgroup . create_for ( pycm . UserType . CURR_USER , [
pycm . RootType . DIR ,
pycm . RootType . DIR_BG ,
pycm . RootType . FILE . format ( FILE_TYPE = ".py" )
])该脚本取决于两个主要类ContextMenuItem和ContextMenuGroup 。
ContextMenuItem这是在ContextMenu中单击时触发并启动命令的菜单项。该课的签名是
ContextMenuItem (
item_name , # name of the item to be shown
command , # command to be executed when selected
item_reg_key = "" , # registry key associated with the item
# (if not given will be treated as item_name)
icon = "" , # path to an icon to be shown with the item
extended = False # set to True if the item is to be shown when right clicked with shift button
).create(root_key: HKEYType) root_key使用get_root实用程序方法获取root_key 。
.create_for(user_type: UserType, root_type: List[RootType])
.delete(root_key: HKEYType) - 在给定的root_key处删除项目。
.delete_for(user_type: UserType, root_type: List[RootType]) - 删除给定用户和根位置的项目。
.delete不是首选,因为可以传递任何root_key 。取而代之的是,请使用.delete_for方法作为删除的注册表键,将自动安全删除。PythonContextMenuItem该类继承了ContextMenuItem ,并将Python函数转换为可执行命令。只需将python函数作为python_function参数传递。
PythonContextMenuItem (
item_name ,
python_function : Callable [[ str ], Any ], # callable python function which should take file/folder name as the single argument
item_reg_key = "" ,
icon = "" ,
extended = False
)ContextMenuGroup此类别分组多个项目和子组。
ContextMenuGroup (
group_name , # name of the group to be shown
group_reg_key = "" , # registry key associated with the group
icon = "" , # path to an icon to be shown with the group
extended = False , # set to True if the group is to be shown when right clicked with shift button
items = [] # items to be displayed on this group
)用于将项目或组添加到组实例呼叫add_item / add_items类的方法。
ContextMenuGroup . add_item ( item )
# or
ContextMenuGroup . add_item ( subgroup )
# for multiple items
ContextMenuGroup . add_items ([ item1 , item2 , subgroup1 , subgroup2 ])ContextMenuGroup对象期间通过items关键字传递项目,则将实现相同的功能。然后创建组并添加到ContextMenu中,只需调用.create以get_root函数作为参数获得的键创建。
ContextMenuGroup.create(root_key) # Create the group and add to contextmenu
该类还具有方法.create , .create_for , .delete和.delete_for方法,该方法与ContextMenuItem的方法相同。
ContextMenuItem和ContextMenuGroup的所有方法都返回self ,因此可以链接它们。除非调用。 .create方法,否则将项目添加到ContextMenuGroup中不会将它们添加到ContextMenu/注册表中。 RootType用于选择上下文菜单项/组的EnumUserType选择是为当前用户还是所有用户添加上下文菜单的Enumget_root(user_type: UserType, root_type: RootType, file_type: str) - 创建/打开所选user_type和root_type的注册表密钥。如果root_type是RootType.FILE ,则需要file_type参数并指示文件扩展。python_script_cmd(script_path, rel_path = False, hide_terminal = False) - 将给定script_path转换为可执行命令的实用程序函数。delete_item(root_key, item_reg_key) - 在给定的root_key registry处删除item_reg_key 。警告:请确保您不会在顶级注册表键上删除键(例如HKEY_CLASSES_ROOT , HKEY_CURRENT_USER等)