Eine einfache und intuitive Möglichkeit, Ihre benutzerdefinierten Skripte zu Ihrem Windows mit der rechten Maustaste auf ContextMenu hinzuzufügen.
Sie können die Datei pywin_contextMenu.py aus diesem Repository (https://raw.githubuSercontent.com/naveennamani/pywin_contextMenu/master/pywin_contextMenu.py) herunterladen und die Datei in Ihrem Script -Ordner platzieren.
Oder Sie können dieses Paket von PYPI verwenden
pip install pywin-contextmenuund einfach in Ihre Skripte importieren.
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" )
]) Das Skript hängt von zwei Hauptklassen ContextMenuItem und ContextMenuGroup ab.
ContextMenuItemDies ist der Menüelement, der den Befehl auslöst und startet, wenn er im Kontextmenu klickt. Die Signatur dieser Klasse ist
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) - Fügt das Element der Registrierung in der angegebenen Registrierung root_key hinzu. Rufen Sie die root_key mit get_root -Dienstprogrammmethode ab.
.create_for(user_type: UserType, root_type: List[RootType]) - fügt die Elemente für einen bestimmten Benutzer und angegebene Root -Positionen hinzu.
.delete(root_key: HKEYType) - Löschen Sie das Element am angegebenen root_key .
.delete_for(user_type: UserType, root_type: List[RootType]) - Löschen Sie die Elemente für den angegebenen Benutzer und die Stammpositionen.
.delete wird nicht bevorzugt, da ein root_key übergeben werden kann. Verwenden Sie stattdessen .delete_for -Methode, da die zu gelöschten Registrierungsschlüssel automatisch und sicher gelöscht werden.PythonContextMenuItem Diese Klasse erbt ContextMenuItem und konvertiert eine Python -Funktion in einen ausführbaren Befehl. Übergeben Sie einfach die Python -Funktion als python_function -Argument.
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
)ContextMenuGroupDiese Klasse gruppiert mehrere Elemente und Untergruppen.
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
) Für das Hinzufügen von Elementen oder Gruppen zu einer Gruppeninstanz rufen Sie add_item / add_items Methode der Klasse auf.
ContextMenuGroup . add_item ( item )
# or
ContextMenuGroup . add_item ( subgroup )
# for multiple items
ContextMenuGroup . add_items ([ item1 , item2 , subgroup1 , subgroup2 ])ContextMenuGroup -Objekten übergeben werden, indem die Elemente mit items übergeben werden. Dann, um die Gruppe zu erstellen und dem Kontext hinzuzufügen, rufen Sie einfach .create mit dem von get_root -Funktion erhaltenen Schlüssel als Argument auf.
ContextMenuGroup.create(root_key) # Create the group and add to contextmenu
Die Klasse hat auch eine Methode .create , .create_for , .delete und .delete_for Methoden, die den der der ContextMenuItem Methoden entsprechen.
ContextMenuItem und ContextMenuGroup geben self zurück, sodass sie angekettet werden können. Das Hinzufügen von Elementen zu ContextMenuGroup fügt sie nicht zum Kontextmenu/der Registrierung hinzu, es sei denn, die Methode .create wird aufgerufen. RootType - Eine Enum zur Auswahl, wo das Kontextmenüelement/die Kontextgruppe angezeigt wirdUserType - Eine Enum zur Auswahl, ob das Kontextmenü für den aktuellen Benutzer oder für alle Benutzer hinzugefügt werden sollget_root(user_type: UserType, root_type: RootType, file_type: str) - Erstellt/öffnet den Registrierungsschlüssel für die ausgewählte user_type und root_type. Wenn das root_type RootType.FILE ist, ist das Argument file_type erforderlich und zeigt die Dateiausweiterung an.python_script_cmd(script_path, rel_path = False, hide_terminal = False) - Eine Dienstprogrammfunktion, um einen bestimmten script_path in einen ausführbaren Befehl zu konvertieren.delete_item(root_key, item_reg_key) - löscht den item_reg_key in der angegebenen root_key -Registrierung. WARNUNG: Bitte stellen Sie sicher, dass Sie die Schlüssel nicht auf den Tasten der obersten Ebene löschen (z. B. HKEY_CLASSES_ROOT , HKEY_CURRENT_USER usw.)