menu.py

# =========================================================
# Simple Menu Template
# ---------------------------------------------------------
# add your own menus and menu items
# =========================================================

import sys
import platform
import os


# --- which Linux version are we running? -----------------

def RunningPython3():
    ##print(sys.version_info)
    if sys.version_info[0] == 3:
        return True
    return False

# --- prompt the user for input ---------------------------

def GetUserInput(prompt,py3):
    if py3:
        return input(prompt)
    else:
        return raw_input(prompt)

# --- pause program ---------------------------------------

def pause(py3):
    print('')
    GetUserInput('Press enter to continue ',py3)

# --- clear the screen ------------------------------------

def ClearScreen():
    if platform.system() == 'Linux':
        os.system('clear')
    elif platform.system() == 'Windows':
        os.system('clear')
    else:
        os.system('cls')

# --- display menu header ---------------------------------

def MenuHeader(title):
    i = 40
    print('-'*i)
    if title != None:
        x = int((i - len(title))/2)
        if  i > 0:
            print(' '*x + title)
        else:
            print(title)
    print('-'*i)

# --- display main menu -----------------------------------

def MainMenu(title):

    while True:
       ClearScreen()
       MenuHeader(title)
       print('  Option  Description')
       print('')
       print('    1     go to menu 1')
       print('    2     go to menu 2')
       print('    3     go to menu 3')
       print('    4     do action  4')
       print('    5     do action  5')
       print('    6     do action  6')
       print('')
       print('   99     exit menu')

       print('')
       option = GetUserInput('    Enter option: ',py3)

       ##print('Input: ({}), length: {}, type: {}'.format( \
       ##      option,len(option),type(option)))

       sopt = option.strip()

       if sopt == '':
           continue

       if sopt.isdigit() != True:
           print('')
           print('Illegal option entered ({})'.format(sopt))
           pause(py3)
           continue

       iopt = int(sopt)

       if iopt == 1:
           Menu1(py3)
           continue
       elif iopt == 2:
           Menu2(py3)
           continue
       elif iopt == 3:
           Menu3(py3)
           continue
       elif iopt == 4:
           Action4(py3)
           continue
       elif iopt == 5:
           Action5(py3)
           continue
       elif iopt == 6:
           Action6(py3)
           continue
       elif iopt == 99:
           break

       print('')
       print('Unknown option selected ({})'.format(option))
       pause(py3)
       continue

def Menu1(py3):
    print('menu1')
    pause(py3)

def Menu2(py3):
    print('Menu2')
    pause(py3)

def Menu3(py3):
    print('Menu3')
    pause(py3)

def Action4(py3):
    print('Action4')
    pause(py3)

def Action5(py):
    print('Action5')
    pause(py3)

def Action6(py):
    print('Action6')
    pause(py3)

# ===========================================================
# main
# ===========================================================

if  __name__ == "__main__":

   ClearScreen()

   py3 = RunningPython3()
   ##print('py3 = {}'.format(py3))

   print('')
   response = GetUserInput("Please Enter Main Menu Title: ",py3)

   MainMenu(response)