Skip to main content

Library to create cross-platform context menus.

Project description

context_menu

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



example usage

Quickstart

  1. Install the library via pip:
python -m pip install context_menu
  1. 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.

The ContextCommand class expects a name, and either a python function, or a shell command, but not both. 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, 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. 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.

Check out the examples folder for more complicated examples.

Types

There are different locations where a context menu can fire. Depending on what you right click, you can get different options. Here's a table that breaks these types down:

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)

Final Words

You can check out the entire documentation here.

I'm extremely pleased with how this project turned out. I can't count how many times I wanted to give up. All the interfaces I had to interact with almost zero documentation (nautilus-python actually had no documentation, but I'm extremely thankful the library existed in the first place).

I wanted to contribute and open up the possibility of using context menus in development to more people, creating this library and documentation for windows.

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-0.0.11.tar.gz (9.1 kB view hashes)

Uploaded Source

Built Distribution

context_menu-0.0.11-py3-none-any.whl (11.4 kB view hashes)

Uploaded Python 3

Supported by

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