Skip to content

Commit

Permalink
simplified menubar arguments, per issue lawsie#390 on original repo. l…
Browse files Browse the repository at this point in the history
  • Loading branch information
hellfire3d committed Apr 14, 2021
1 parent 9938664 commit e60027a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
38 changes: 19 additions & 19 deletions guizero/MenuBar.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,27 @@

class MenuBar(Component):

def __init__(self, master, toplevel, options):
def __init__(self, master, menudict):

"""
Creates a MenuBar
:param Container master:
The Container (App, Box, etc) the MenuBar will belong too.
:param List toplevel:
A list of strings to populate the top level menu options.
:param List options:
A 3D list of:
- submenus,
- with each submenu being a list of options
- and each option being a text/command pair
:param Dict menudict
A dictionary that contains all of the menus and their sub-menus
e.g ::
options=[
[ ["File option 1", file_function], ["File option 2", file_function] ],
[ ["Edit option 1", edit_function], ["Edit option 2", edit_function] ]
]
{
"foo": {
"foo1": callback,
"foo2": callback
},
"bar": {
"bar1": callback,
"bar2": callback
}
}
"""

if not isinstance(master, (App, Window)):
Expand All @@ -44,19 +42,21 @@ def __init__(self, master, toplevel, options):
self._sub_menus = []

# Create all the top level menus
for i in range(len(toplevel)):
i = 0
for menu_item in menudict:
# Create this submenu
new_menu = Menu(self.tk, tearoff=0)

# Populate the drop down menu with the items/commands from the list
for menu_item in options[i]:
new_menu.add_command(label=menu_item[0], command=menu_item[1])
for k in menudict[menu_item]:
new_menu.add_command(label=k, command=menudict[menu_item].get(k))

# Append to the submenus list
self._sub_menus.append(new_menu)

# Add to the menu bar
self.tk.add_cascade(label=toplevel[i], menu=self._sub_menus[i])
self.tk.add_cascade(label=menu_item, menu=self._sub_menus[i])
i+=1

# Set this as the menu for the master object
master.tk.config(menu=self.tk)
Expand Down
26 changes: 19 additions & 7 deletions tests/test_menubar.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,25 @@ def test_initial_values():
def callback():
callback_event.set()

m = MenuBar(
a,
["foo", "bar"],
[
[ ["foo1", callback], ["foo2", callback] ],
[ ["bar1", callback], ["bar2", callback] ]
])
#m = MenuBar(
# a,
# ["foo", "bar"],
# [
# [ ["foo1", callback], ["foo2", callback] ],
# [ ["bar1", callback], ["bar2", callback] ]
# ])

m = MenuBar(a,
{
"foo": {
"foo1": callback,
"foo2": callback
},
"bar": {
"bar1": callback,
"bar2": callback
}
})

assert m.master == a

Expand Down

0 comments on commit e60027a

Please sign in to comment.