Welcome to quick-menu
This is a simple package to create text menus for use in console applications.
API
Provide a simple means to create text menus for a console application.
Example
This example shows two ways to create a menu, one for the submenu and the other for the main menu.
from quick_menu.menu import Menu, MenuItem
def func1(val=1):
print("func1: val =", val)
input("Press [Enter] to continue")
submenu = Menu(
"Submenu",
menu_items=[
MenuItem("1", "Do func1", action=func1),
MenuItem("X", "Go back", is_exit=True),
]
)
menu = (
Menu("Some Title")
.add(MenuItem("1", "Func1 default", action=func1))
.add(MenuItem("2", "Func1 with val", action=func1, action_args={"val": 4}))
.add(MenuItem("S", "Submenu", action=submenu.run))
.add(MenuItem("X", "Exit", is_exit=True))
)
menu.run()
Menu
A menu that can be displayed and accepts user selections.
Source code in src\quick_menu\menu.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
__init__(title, menu_items=None, prompt=None, auto_clear=None)
Create a new menu.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to display at the top of the menu. |
required |
menu_items |
Optional[List[MenuItem]]
|
An optional list of |
None
|
prompt |
Optional[str]
|
An optional custom prompt for input. |
None
|
auto_clear |
Optional[bool]
|
Whether or not to clear the screen before running. |
None
|
Source code in src\quick_menu\menu.py
add(menu_item)
Add a new MenuItemq.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
menu_item |
MenuItem
|
A |
required |
Returns:
Type | Description |
---|---|
Self
|
The
|
Source code in src\quick_menu\menu.py
clear()
staticmethod
display()
Return the Menu display as a string.
Source code in src\quick_menu\menu.py
run()
Display the Menu
and start a loop to process selections.
Source code in src\quick_menu\menu.py
MenuItem
dataclass
A menu item to add to a menu.
The menu item can optionally call a function or start a submenu. A menu item can also be an exit item which exits the curren menu.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
choice |
str
|
The string used to select the menu item. |
required |
label |
str
|
The text label displayed for the menu item. |
required |
action |
Optional[Callable[..., None]]
|
An optional function to be called when the menu item is selected. |
None
|
action_args |
dict
|
Arguments to pass to a menu item action. |
field(default_factory=dict)
|
is_exit |
bool
|
Whether or not this menu item exits the current menu |
False
|
Source code in src\quick_menu\menu.py
select()
Select the menu item.
This selects the MenuItem
which runs any associated action with kwargs and
then returns whether or not selecting this item should tell the current menu to
exit.
Returns:
Type | Description |
---|---|
bool
|
True if the item is an exit item; False otherwise |
Source code in src\quick_menu\menu.py
update(**kwargs)
Update menu item fields.
Any key matching a field to be updated is replaced with the new vlaue. Any
remaining key/value pairs are used to update the action_args
.
Paramaters
kwargs: A dict with fields to be updated.