Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
menu while loop
#8
(Dec-20-2021, 07:09 AM)Gribouillis Wrote: @BashBedlam With very little work, you could make Menu_Tree.run_menus() non recursive. Also add the possibility that after command(), the menu to run is not the main_menu.
This would be my best shot at that Cool
def dummy_function () :
	print ('\n   The dummy function has finished running.')

def say_goodbye () :
	print ('\nBye for now.')
	exit ()

class Menu_Tree :
	def __init__ (self) :
		self.all_menus = {
			'main_menu' : {
				"[1] Products": 'products_menu',
				"[2] Clients": 'clients_menu',
				"[3] Display": 'display_menu',
				'[4] Report': 'report_menu',
				"[0] Exit the program.": say_goodbye},
			'products_menu' : {
				"[1] Add new product": dummy_function,
				"[2] Product update": dummy_function,
				"[3] Delete product": dummy_function,
				'[4] Go back to main page': 'main_menu',
				"[0] Exit the program.": say_goodbye},
			'clients_menu' : {
				"[1] Add new client": dummy_function,
				"[2] Client update": dummy_function,
				"[3] Delete client": dummy_function,
				'[4] Go back to main page': 'main_menu',
				"[0] Exit the program.": say_goodbye},
			'display_menu' : {
				"[1] Search list of: ": dummy_function,
				"[2] Search a: ": dummy_function,
				'[4] Go back to main page': 'main_menu',
				"[0] Exit the program.": say_goodbye},
			'report_menu' : {
				"[1] Average age of clients: ": dummy_function,
				"[2] The average price of the products ": dummy_function,
				'[4] Return to the main menu': 'main_menu',
				"[0] Exit the program.": say_goodbye}}

		self.run_the_current_command ('main_menu')

	def run_the_current_command (self, command) :
		if type (command) == str :
			self.current_menu = command
			command = self.run_the_menus (self.all_menus [command])
		else :
			command ()
			self.run_the_menus (self.all_menus [self.current_menu])

	def run_the_menus (self, menu) :
		look_up_table = {label [1: 2]: label for label in menu}

		print ('\n   ==============================')
		for label in menu :
			print ('     ', label)

		still_selecting = True
		while  still_selecting :
			menu_choice = input ('   Enter your selection: ')
			if menu_choice != '' and menu_choice in look_up_table :
				still_selecting = False
				command = menu [look_up_table [menu_choice]]
			else :
				print (f'     "{menu_choice}" is not a valid entry.')

		self.run_the_current_command (command)

if __name__ == '__main__' :
	Menu_Tree ()
Larz60+ likes this post
Reply


Messages In This Thread
menu while loop - by 3lnyn0 - Dec-17-2021, 03:13 PM
RE: menu while loop - by BashBedlam - Dec-17-2021, 07:03 PM
RE: menu while loop - by 3lnyn0 - Dec-28-2021, 12:58 PM
RE: menu while loop - by Pedroski55 - Dec-18-2021, 03:56 AM
RE: menu while loop - by 3lnyn0 - Dec-21-2021, 07:17 PM
RE: menu while loop - by snippsat - Dec-18-2021, 10:59 AM
RE: menu while loop - by Gribouillis - Dec-19-2021, 10:59 AM
RE: menu while loop - by BashBedlam - Dec-20-2021, 03:24 AM
RE: menu while loop - by Gribouillis - Dec-20-2021, 07:09 AM
RE: menu while loop - by BashBedlam - Dec-20-2021, 09:32 PM
RE: menu while loop - by bowlofred - Dec-21-2021, 08:06 PM
RE: menu while loop - by Pedroski55 - Dec-22-2021, 04:30 AM
RE: menu while loop - by BashBedlam - Dec-28-2021, 08:04 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020