طريقة بسيطة وبديهية لإضافة البرامج النصية المخصصة إلى Windows انقر بزر الماوس الأيمن فوق ContextMenu.
يمكنك تنزيل ملف pywin_contextmenu.py من هذا المستودع (https://raw.githubusercontent.com/naveennamani/pywin_contextmen/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 . الحصول على root_key باستخدام طريقة الأداة المساعدة get_root .
.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 cate مع المفتاح الذي تم الحصول عليه من وظيفة 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 إلخ)