Cara sederhana dan intuitif untuk menambahkan skrip kustom Anda ke windows Anda klik kanan ContextMenu.
Anda dapat mengunduh file pywin_contextMenu.py dari repositori ini (https://raw.githubusercontent.com/naveennamani/pywin_contextmenu/master/pywin_contextmenu.py) dan letakkan file di folder skrip Anda.
Atau Anda dapat menginstal paket ini dari PYPI menggunakan
pip install pywin-contextmenudan cukup impor di skrip Anda.
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" )
]) Script tergantung pada dua kelas utama ContextMenuItem dan ContextMenuGroup .
ContextMenuItemIni adalah item menu yang memicu dan meluncurkan perintah saat diklik di ContextMenu. Tanda tangan kelas ini adalah
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) - menambahkan item ke registri di registry root_key yang diberikan. Dapatkan root_key menggunakan metode utilitas get_root .
.create_for(user_type: UserType, root_type: List[RootType]) - menambahkan item untuk pengguna yang diberikan dan lokasi root yang diberikan.
.delete(root_key: HKEYType) - Hapus item di root_key yang diberikan.
.delete_for(user_type: UserType, root_type: List[RootType]) - hapus item untuk pengguna yang diberikan dan lokasi root.
.delete tidak disukai karena root_key apa pun dapat dilewati. Alih -alih, silakan gunakan metode .delete_for sebagai tombol registri yang akan dihapus akan secara otomatis dan aman dihapus.PythonContextMenuItem Kelas ini mewarisi ContextMenuItem dan mengubah fungsi Python ke perintah yang dapat dieksekusi. Cukup lulus fungsi Python sebagai argumen 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
)ContextMenuGroupKelas ini mengelompokkan beberapa item dan subkelompok.
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
) Untuk menambahkan item atau grup ke instance grup panggilan add_item / add_items metode kelas.
ContextMenuGroup . add_item ( item )
# or
ContextMenuGroup . add_item ( subgroup )
# for multiple items
ContextMenuGroup . add_items ([ item1 , item2 , subgroup1 , subgroup2 ])ContextMenuGroup dengan meneruskan item menggunakan kata kunci items . Kemudian untuk membuat grup dan menambahkan ke ContextMenu cukup panggil .create Buat dengan kunci yang diperoleh dari fungsi get_root sebagai argumen.
ContextMenuGroup.create(root_key) # Create the group and add to contextmenu
Kelas juga memiliki metode .create , .create_for , .delete dan .delete_for Metode yang sama dengan metode dari ContextMenuItem .
ContextMenuItem dan ContextMenuGroup mengembalikan self , sehingga mereka dapat dirantai. Menambahkan item ke ContextMenuGroup tidak akan menambahkannya ke ContextMenu/Registry kecuali jika metode .create dipanggil. RootType - Enum untuk memilih di mana item/grup menu konteks akan ditampilkanUserType - Enum untuk memilih apakah akan menambahkan menu konteks untuk pengguna saat ini atau untuk semua penggunaget_root(user_type: UserType, root_type: RootType, file_type: str) - Membuat/membuka kunci registri untuk user_type dan root_type yang dipilih. Jika root_type adalah RootType.FILE maka argumen file_type diperlukan dan menunjukkan ekstensi file.python_script_cmd(script_path, rel_path = False, hide_terminal = False) - fungsi utilitas untuk mengonversi script_path yang diberikan ke perintah yang dapat dieksekusi.delete_item(root_key, item_reg_key) - Menghapus item_reg_key pada registry root_key yang diberikan. PERINGATAN: Harap pastikan Anda tidak menghapus kunci di tombol registri tingkat atas (misalnya HKEY_CLASSES_ROOT , HKEY_CURRENT_USER dll.)