Package: kivy-garden.contextmenu
Kivy Context MenuCollection of classes for easy creatingcontextandapplicationmenus.Please see the gardeninstructionsfor how to use kivy garden flowers.Flower informationContext MenuContext menu is represented byContextMenuwidget that wraps all menu items asContextMenuTextItemwidgets. Context menus can be nested, eachContextMenuTextItemcan contain maximum oneContextMenuwidget.importkivyfromkivy.appimportAppfromkivy.langimportBuilderimportkivy_garden.contextmenukv="""FloatLayout:id: layoutLabel:pos: 10, self.parent.height - self.height - 10text: "Left click anywhere outside the context menu to close it"size_hint: None, Nonesize: self.texture_sizeButton:size_hint: None, Nonepos_hint: {"center_x": 0.5, "center_y": 0.8 }size: 300, 40text: "Click me to show the context menu"on_release: context_menu.show(*app.root_window.mouse_pos)ContextMenu:id: context_menuvisible: Falsecancel_handler_widget: layoutContextMenuTextItem:text: "SubMenu #2"ContextMenuTextItem:text: "SubMenu #3"ContextMenu:ContextMenuTextItem:text: "SubMenu #5"ContextMenuTextItem:text: "SubMenu #6"ContextMenu:ContextMenuTextItem:text: "SubMenu #9"ContextMenuTextItem:text: "SubMenu #10"ContextMenuTextItem:text: "SubMenu #11"ContextMenuTextItem:text: "Hello, World!"on_release: app.say_hello(self.text)ContextMenuTextItem:text: "SubMenu #12"ContextMenuTextItem:text: "SubMenu #7"ContextMenuTextItem:text: "SubMenu #4""""classMyApp(App):defbuild(self):self.title='Simple context menu example'returnBuilder.load_string(kv)defsay_hello(self,text):print(text)self.root.ids['context_menu'].hide()if__name__=='__main__':MyApp().run()Arrows that symbolize that an item has sub menu is created automatically.ContextMenuTextIteminherits fromButtonBehaviorso you can useon_releaseto bind actions to it.The root context menu can usecancel_handler_widgetparameter. This addson_touch_downevent to it that closes the menu when you click anywhere outside the menu.Application MenuCreating application menus is very similar to context menus. UseAppMenuandAppMenuTextItemwidgets to create the top level menu. Then eachAppMenuTextItemcan contain oneContextMenuwidget as we saw above.AppMenuTextItemwithoutContextMenuare disabled by defaultimportkivyfromkivy.appimportAppfromkivy.langimportBuilderimportkivy_garden.contextmenukv="""FloatLayout:id: layoutAppMenu:id: app_menutop: root.heightcancel_handler_widget: layoutAppMenuTextItem:text: "Menu #1"ContextMenu:ContextMenuTextItem:text: "Item #11"ContextMenuTextItem:text: "Item #12"AppMenuTextItem:text: "Menu Menu Menu #2"ContextMenu:ContextMenuTextItem:text: "Item #21"ContextMenuTextItem:text: "Item #22"ContextMenuTextItem:text: "ItemItemItem #23"ContextMenuTextItem:text: "Item #24"ContextMenu:ContextMenuTextItem:text: "Item #241"ContextMenuTextItem:text: "Hello, World!"on_release: app.say_hello(self.text)# ...ContextMenuTextItem:text: "Item #5"AppMenuTextItem:text: "Menu Menu #3"ContextMenu:ContextMenuTextItem:text: "SubMenu #31"ContextMenuDivider:ContextMenuTextItem:text: "SubMenu #32"# ...AppMenuTextItem:text: "Menu #4"# ...# The rest follows as usually"""classMyApp(App):defbuild(self):self.title='Simple app menu example'returnBuilder.load_string(kv)defsay_hello(self,text):print(text)self.root.ids['app_menu'].close_all()if__name__=='__main__':MyApp().run()Installpipinstallkivy_garden.contextmenuUsageAll classesgarden.contextmenuprovides you with a set of classes and mixins for creating your own customised menu items for both context and application menus.context_menu.AbstractMenuMixin class that represents basic functionality for all menus. It cannot be used by itself and needs to be extended with a layout. Providescancel_handler_widgetproperty. SeeAppMenuorContextMenu.context_menu.ContextMenuImplementation of a context menu.context_menu.AbstractMenuItemMixin class that represents a single menu item. Needs to be extended to be any useful. It's a base class for all menu items for both context and application menus.If you want to extend this class you need to override thecontent_widthproperty which tells the parentContextMenuwhat is the expected width of this item. It needs to know this to set it's own width.context_menu.ContextMenuItemSingle context menu item. Automatically draws an arrow if contains aContextMenuchildren. If you want to create a custom menu item extend this class.context_menu.AbstractMenuItemHoverableMixin class that makes any class that inheritsContextMenuItemto change background color on mouse hover.context_menu.ContextMenuTextMenu item withLabelwidget without any extra functionality.context_menu.ContextMenuDividerMenu widget that splits two parts of a context/app menu.It also contains an instance ofLabelwhich is not visible if you don't set it any text.ContextMenuTextItem:text:"SubMenu #33"ContextMenuDivider:text:"More options"ContextMenuTextItem:text:"SubMenu #34"context_menu.ContextMenuTextItemMenu item with text. You'll be most of the time just fine using this class for all your menu items. You can also see it used inall examples here.  Contains aLabelwidget and copiestext,font_sizeandcolorproperties to it automatically.app_menu.AppMenuApplication menu widget. By default it fills the entire parent's width.app_menu.AppMenuTextItemApplication menu item width text. Contains aLabelwidget and copiestext,font_sizeandcolorproperties to it automatically.ContributingCheck out ourcontribution guideand feel free to improve the flower.LicenseThis software is released under the terms of the MIT License.
Please see theLICENSE.txtfile.