วิธีที่ง่ายและใช้งานง่ายในการเพิ่มสคริปต์ที่กำหนดเองของคุณไปยัง Windows คลิกขวาบริบทของคุณ
คุณสามารถดาวน์โหลดไฟล์ pywin_contextmenu.py ได้จากที่เก็บนี้ (https://raw.githubusercontent.com/naveennamani/pywin_contextmenu/master/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นี่คือรายการเมนูที่เรียกและเรียกใช้คำสั่งเมื่อคลิกในบริบท ลายเซ็นของคลาสนี้คือ
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 รับ root_key โดยใช้วิธี get_root Utility
.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 จากนั้นเพื่อสร้างกลุ่มและเพิ่มลงในบริบทเพียงแค่เรียกใช้ .create สร้างด้วยคีย์ที่ได้จากฟังก์ชัน get_root เป็นอาร์กิวเมนต์
ContextMenuGroup.create(root_key) # Create the group and add to contextmenu
ชั้นเรียนยังมีวิธีการ .create , .create_for , .delete และ .delete_for ซึ่งเป็นวิธีเดียวกับวิธีการของ ContextMenuItem
ContextMenuItem และ ContextMenuGroup กลับมา self เพื่อให้สามารถถูกล่ามโซ่ได้ การเพิ่มรายการลงใน ContextMenuGroup จะไม่เพิ่มลงในบริบท/รีจิสทรีเว้นแต่จะมีการเรียกใช้วิธี .create . RootType - Enum สำหรับการเลือกที่รายการเมนูบริบท/กลุ่มจะปรากฏขึ้นUserType - Enum สำหรับการเลือกว่าจะเพิ่มเมนูบริบทสำหรับผู้ใช้ปัจจุบันหรือสำหรับผู้ใช้ทั้งหมดget_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) - ลบ item_reg_key ที่รีจิสทรี root_key ที่กำหนด คำเตือน: โปรดตรวจสอบให้แน่ใจว่าคุณไม่ได้ลบคีย์ที่คีย์รีจิสทรีระดับบนสุด (เช่น HKEY_CLASSES_ROOT , HKEY_CURRENT_USER เป็นต้น)