Skip to main content

Library to create cross-platform native context menus.

Project description

💻 context_menu 💻

A Python library to create and deploy cross-platform native context menus.

build passing readthedocs pip python version


example usage

Quickstart

  1. Install the library via pip:

    python -m pip install context_menu

  2. Create and compile the menu:

  • You can create items in as little as 3 lines:
    from context_menu import menus
    fc = menus.FastCommand('Example Fast Command 1', type='FILES', command='echo Hello')
    fc.compile()
    
  • Or you can create much more complicated nested menus:
        def foo2(filenames):
            print('foo2')
            print(filenames)
            input()

        def foo3(filenames):
            print('foo3')
            print(filenames)
            input()

        if __name__ == '__main__':
            from context_menu import menus

            cm = menus.ContextMenu('Foo menu', type='FILES')
            cm2 = menus.ContextMenu('Foo Menu 2')
            cm3 = menus.ContextMenu('Foo Menu 3')

            cm3.add_items([
                menus.ContextCommand('Foo One', command='echo hello > example.txt'),
            ])
            cm2.add_items([
                menus.ContextCommand('Foo Two', python=foo2),
                cm3,
            ])
            cm.add_items([
                cm2,
                menus.ContextCommand('Foo Three', python=foo3)
            ])

            cm.compile()
  1. See the output!
  • First example

    first Example

  • Second example

    second Example

Detailed Usage

The ContextMenu class expects a name, and the activation type if it is the root menu(the first menu). Only compile the root menu.

ContextMenu(name: str, type: str = None)

The ContextCommand class expects a name, and either a python function, or a shell command, but not both.

ContextCommand(name: str, command: str = None, python: function = None)

A ContextCommand is the selectable element of a context menu (you can click this part). Python functions can be passed to this method, regardless of their location. However, the function must accept only one parameter filenames, which is a list of paths, and if the function is in the same file as the menu, you have to surround it with if __name__ == '__main__':

Any command passed (as a string) will be directly ran from the shell.

def foo2(filenames):
    print('foo2')
    print(filenames)
    input()

def foo3(filenames):
    print('foo3')
    print(filenames)
    input()


if __name__ == '__main__':
    from context_menu import menus

    cm = menus.ContextMenu('Foo menu', type='DIRECTORY_BACKGROUND')
    cm.add_items([
        menus.ContextCommand('Foo One', command='echo hello > example.txt'),
        menus.ContextCommand('Foo Two', python=foo2),
        menus.ContextCommand('Foo Three', python=foo3)
    ])
    cm.compile()

The FastCommand class is an extension of the ContextMenu class and allows you to quickly create a single entry menu.

FastCommand(name: str, type: str, command: str = None, python: function = None)

It expects a name, type, and command/function.

def foo1(filenames):
    print(filenames)
    input()

if __name__ == '__main__':
    from context_menu import menus

    fc = menus.FastCommand('Example Fast Command 1', type='FILES', python=foo1)
    fc.compile()

You can use the {MENU}.add_items{ITEMS} function to add these elements together. Menus can be added to menus, creating cascading context menus. You have to call {MENU}.compile() in order to create the menu. Admin privileges are required on windows, as it modifies the Registry. The code will automatically prompt for Admin rights if it is not sufficiently elevated.

You can remove a menu easily as well. Simply call the 'menus.removeMenu()' method.

removeMenu(name: str, type: str)

For example, if I wanted to remove the menu 'Foo Menu' that activated on type 'FILES':

from context_menu import menus

menus.removeMenu('Foo Menu', 'FILES')

Check out the examples folder for more complicated examples.

Types

There are different locations where a context menu can fire (if you right click on a folder you'll get different options then if you right click on a file). The type variable controls this behavior in the library, and you can reference this table to determine the type:

Name Location Action
FILES HKEY_CLASSES_ROOT*\shell\ Opens on a file
DIRECTORY HKEY_CLASSES_ROOT\Directory\shell Opens on a directory
DIRECTORY_BACKGROUND HKEY_CLASSES_ROOT\Directory\Background\shell Opens on the background of the Directory
DESKTOP_BACKGROUND HKEY_CLASSES_ROOT\DesktopBackground\Shell Opens on the background of the Desktop
DRIVE HKEY_CLASSES_ROOT\Drive\shell Opens on the drives(think USBs)

Important notes

  • The code can sometimes be really weird when it comes to admin rights on Windows. The compile() method will automatically prompt for admin rights if required, but this can cause issues sometimes.

  • Almost all of the errors I've encountered in testing were when the code and the functions were in the same file. You should make a separate file for the code or surround it with if __name__ == '__main__':.

Feel free to check out a file sorter I made that implements very complex usage of this library.

You can check out the official documentation here.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

context_menu-1.0.2.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

context_menu-1.0.2-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file context_menu-1.0.2.tar.gz.

File metadata

  • Download URL: context_menu-1.0.2.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.8

File hashes

Hashes for context_menu-1.0.2.tar.gz
Algorithm Hash digest
SHA256 b252f8488bae44aca55bd486cd8743340b2bdcebb4b8584a22229845a128bcea
MD5 dc347470efd7bb99f8df2b8aa101d44a
BLAKE2b-256 2d35f330e9af94794175e4b8d18680d3d27cfec72f8f9c574f138d84e953596d

See more details on using hashes here.

File details

Details for the file context_menu-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: context_menu-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.8

File hashes

Hashes for context_menu-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 00392cb105d18eaaddb75c3f042fc5a85f16226ff5d6d591a3e4480b94346f19
MD5 4878303b9705d1519821a17b0173a595
BLAKE2b-256 fe3d1f2159e5d5383c61961ede4dc44eed3ea3e750abbbdbad160cf010d83fda

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page