Introduction

Installation

Limitations of the "file" protocol

Frequently asked questions

Syntax, keywords and built-in functions

Standard distribution

import implementation

Brython packages

Browser interface

Introduction - DOM API
Creating a document
Accessing elements
Attributes, properties and methods

Events
Mouse events
Keyboard events
Focus events
Drag events

Query string

Interactions with Javascript

Brython-specific built-in modules

browser
browser.aio
browser.ajax
browser.html
browser.local_storage
browser.markdown
browser.object_storage
browser.session_storage
browser.svg
browser.template
browser.timer
browser.webcomponent
browser.websocket
browser.worker

Widgets
browser.widgets.dialog
browser.widgets.menu

interpreter
javascript

Working with Brython

Execution options
Testing and debugging
Deploying an application

Cookbook

Hello world !
Insert content in an element
HTML markup (bold,italic...)
HTML table
Bind and unbind events
Handle options in a SELECT
Drag and drop
Get the content of an element
Read the content of a file
Store objects locally
Example of onmouseover
 

browser.widgets.menu

This module is made to build a menu, made of an horizontal navigation bar and drop-down submenus.

It defines a single class:

Menu(container=document.body, default_css=True)
return a menu object

  • container is the element the navigation bar is inserted into
  • default_css specifies if the default style sheet provided by the module should be used (cf. "CSS styling" below)

Instances of class Menu have the following methods:

Menu.add_item(label, callback=None)

add an item to the menu and returns it

  • label is the element text
  • if callback is provided, it is the function called when the item is clicked on. This function takes a single argument, an object of type event

Menu.add_link(label, href)

add a link (HTML tag <A>) to the menu and return this element

  • label is the link text
  • href is the address associated with the link

Menu.add_menu(label)

add a submenu to the current menu and returns it; the submenu is also an instance of the class Menu, so that other items and submenus can be added with the same methods

  • label is the name of the submenu, printed in the current menu

Example

from browser import document, html
from browser.widgets.menu import Menu

zone = document["zone"]
menu = Menu(zone)
file_menu = menu.add_menu("File")

save_menu = file_menu.add_menu("Save")
choice1 = save_menu.add_menu("choice 1")
choice1.add_item("sub-choice 1")
choice1.add_item("sub-choice 2")
save_menu.add_item("choice 2")

file_menu.add_item("Open")
save_menu = file_menu.add_menu("Properties")
save_menu.add_item("size")
save_menu.add_item("security")

file_menu.add_item("Print")

edit_menu = menu.add_menu("Edition")
edit_menu.add_item("Search")

CSS styling

If the argument default_css passed to Menu() is True (default value), the following style sheet is inserted in the current HTML page:

/* Classes for brython.widgets.menu */

:root {
  --brython-menu-font-family: Arial;
  --brython-menu-font-size: 100%;
  --brython-menu-navbar-bgcolor: CadetBlue;
  --brython-menu-navbar-bgcolor-selected: SkyBlue;
  --brython-menu-navbar-color: #fff;
  --brython-menu-color: #000;
  --brython-menu-submenu-bgcolor: #fff;
  --brython-menu-submenu-bgcolor-selected: SkyBlue;
}

/* Items in the main horizontal navigation bar */
.brython-menu-navbar-item {
    font-family: var(--brython-menu-font-family);
    font-size: var(--brython-menu-font-size);
    background-color: var(--brython-menu-navbar-bgcolor);
    color: var(--brython-menu-navbar-color);
    padding: 0.5em 1em 0.5em 1em;
    cursor: default;
}

.brython-menu-navbar-item:hover {
    background-color: var(--brython-menu-navbar-bgcolor-selected);
}

.brython-menu-navbar-item-selected {
    background-color: var(--brython-menu-navbar-bgcolor-selected);
}

/* submenu, opened by a click on an item */
.brython-menu-submenu {
    font-family: var(--brython-menu-font-family);
    font-size: var(--brython-menu-font-size);
    background-color: var(--brython-menu-submenu-bgcolor);
    position: absolute;
    border-style: solid;
    border-width: 1px;
    border-color: var(--brython-menu-color);
    border-spacing: 0;
}

/* submenu row */
.brython-menu-submenu-row:hover {
    color: var(--brython-menu-color);
    background-color: var(--brython-menu-submenu-bgcolor-selected);
}

.brython-menu-submenu-row-selected {
    color: var(--brython-menu-color);
    background-color: var(--brython-menu-submenu-bgcolor-selected);
}

/*
   cell in a submenu row. Each row has two cells, one for the item label, the
   other one filled with a > if the item has a submenu
*/
.brython-menu-submenu-item {
    font-family: var(--brython-menu-font-family);
    padding: 0.3em 0.3em 0.3em 1em;
    cursor: default;
}

/* end of browser.widgets.menu classes */

To customize the menu, the argument default_css is set to False and the menu classes must be redefined. The most straightforward is to copy the stylesheet above and edit it.