def Build(pFrame, pMenuName, pFileName, pLabelList, pSubmit=submitnow, pCallBack=None,
              pDefaultLabel='yellow', pDefaultButton='light blue', pSide=TOP, pFill=X):
    '''
    Build menu from menu file
    The menu file may contain commands to set the current directory and to
    set specified environment variables.  This is done while the file is
    being read.  They are not dependent on the selected button.  The last 
    command encountered will be active.

    set environment variable name to value
        ;#set`name=value
    set current directory to path
        ;#cd`path
    set label background color
        ;#label`color
    set button background color
        ;#button`color
    '''
#
    lPos = pFileName.rfind('\\')
    if lPos >= 0:
        lFileName = pFileName[lPos+1:].lower()
    else:
        lFileName = pFileName.lower()

    lExist = os.path.exists(pFileName)
        
    lFile = open(pMenuName,'r')
    lFileText = lFile.readlines()
    lFile.close

    lDefaultButton = lButtonColor = pDefaultButton
    lDefaultLabel = lLabelColor = pDefaultLabel
    label = Tkinter.Label(pFrame, text=pFileName, bg=lLabelColor)
    #label.pack(side=TOP, fill=X)
    label.pack(side=pSide, fill=pFill)
    lKeep = False
    lSectionCount = 0
   
    for lText in lFileText:
        if len(lText) > 1:
            if lText[0] != "[":

#               ---- Menu splitter line
                if lText[0] == "-" and lKeep:
                    if len(lText) > 1:
                        lPrompt = lText[1:-1]
                        if lPrompt.find('{') >= 0:
                            lPrompt = ExpandCmd(lPrompt, pFileName)
                                
#                   ---- Display label for - line                                
#                            can be removed without affecting remainder of program
                    if len(lPrompt) > 0 and lKeep == True:
                        label= Tkinter.Label(pFrame, text=lPrompt, bg=lLabelColor)
                        #label.pack(side=TOP, fill=X)
                        label.pack(side=pSide, fill=pFill)

#               ---- process special command
                elif lText.startswith(";#") and (lSectionCount == 0 or lKeep == True): 
                    lPos = lText.find("`")          # <---- Fields are separated by ` (below ~)
                    if lPos > 0:
                        lCh = lText[2]

#                       ---- Set environment variable
                        if lCh == 's':              # set environment variable
                            lEqPos = lText.find("=")
                            if lEqPos > lPos:
                                lKey = lText[lPos+1:lEqPos].strip()
                                lValue = lText[lEqPos+1:].strip()
                                if lValue.find('{') >= 0:
                                    lValue = ExpandCmd(lValue, pFileName)
                                os.environ[lKey] = lValue

#                       ---- Change working directory
                        elif lCh == 'c':            # set working directory
                            if lPos > 0 and lPos < len(lText):
                                lText = lText[lPos+1:].strip()
                                if lText.find('{') >= 0:
                                    lText = ExpandCmd(lText, pFileName)
                                os.chdir(lText)
                                
                        elif lCh == 'b':
                            lButtonColor = lText[lPos+1:].strip().lower()
                            if lSectionCount == 0:
                                lDefaultButton = lButtonColor

                        elif lCh == 'l':
                            lLabelColor = lText[lPos+1:].strip().lower()
                            if lSectionCount == 0:
                                lDefaultLabel = lLabelColor

                        elif pCallBack != None:
                            pCallBack(lText)
                            
                        else:                       # ignore
                            pass 

#               ---- Comment                            
                elif lText[0] == ";":
                    pass

#               ---- Menu section is being skipped
                elif lKeep == False:
                    pass

#               ---- Command template
                else:
                    lPos = lText.find(",")
                    if lPos > 0:
                        lPrompt = lText[:lPos]
                        lCommand = lText[lPos+1:]

#                       ---- Filter commands based on match with file name
#                            /*3.py/Run,c:\source\python31\pyrhonw.exe ...
#                            can be removed without affecting remainder of program
                        if lPrompt[0] == '/':
                            try:
                                (lMask, lPrompt) = lPrompt[1:].split('/')
                                if not fnmatch.fnmatch(lFileName, lMask.lower()):
                                    continue
                            except Exception, e:
                                continue
                                
#                       ---- Filter commands based on mismatch with file name
#                            !*3.py!Run,c:\source\python27\pythonw.exe ...
#                            can be removed without affecting remainder of program
                        elif lPrompt[0] == '!':
                            try:
                                (lMask, lPrompt) = lPrompt[1:].split('!')
                                if fnmatch.fnmatch(lFileName, lMask.lower()):
                                    continue
                            except Exception, e:
                                continue

#                       ---- Create toolbar button                        
                        mCmds[lPrompt] = lCommand
                        try:
                            button = Tkinter.Button(pFrame, text=lPrompt, bg=lButtonColor)
                        except:
                            button = Tkinter.Button(pFrame, text=lPrompt)
                        #button.pack(side=TOP,fill=X)
                        button.pack(side=pSide, fill=pFill)
                        button.bind("<Button-1>", pSubmit) ### (1)
                        button.bind("<Button-3>", showit)

#           ---- Section Header
            else:
                lSectionCount += 1
                lLabelColor = lDefaultLabel
                lButtonColor = lDefaultButton
                if lText.find('{') >= 0:
                    lText = ExpandCmd(label, pFileName)
                label = lText[1:-2].lower()

#               ---- Filter section based on match with file name
#                    [/*3.py/Special]
#                    can be removed without affecting remainder of program
                if label[0] == '/':
                    try:
                        (lMask, label) = label[1:].split('/')
                        if not fnmatch.fnmatch(lFileName, lMask.lower()):
                            lKeep = False
                            continue
                    except Exception, e:
                        lKeep = False
                        continue
                        
#               ---- Filter section based on mismatch with file name
#                    [!*3.py!Special]
#                    can be removed without affecting remainder of program
                elif label[0] == '!':
                    try:
                        (lMask, label) = label[1:].split('!')
                        if fnmatch.fnmatch(lFileName, lMask.lower()):
                            lKeep = False
                            continue
                    except Exception, e:
                        lKeep = False
                        continue

#               ---- label must begin with letter
                elif (label[0] < 'a') or (label[0] > 'z'):
                    label = label[1:]

#               ---- Select/Unselect section                    
                if pLabelList == [] or label in pLabelList:
                    lKeep = True
                    label = Tkinter.Label(pFrame,text=label.capitalize(), bg=lLabelColor)
                    #label.pack(side=TOP, fill=X)
                    label.pack(side=pSide, fill=pFill)
                else:
                    lKeep = False
                    
    label = Tkinter.Label(pFrame,text="", bg=lLabelColor)
    #label.pack(side=TOP, fill=X)
    label.pack(side=pSide, fill=pFill)
    button = Tkinter.Button(pFrame,text='Exit', command=pFrame.quit, bg=lButtonColor)
    #button.pack(side=TOP, fill=X)
    button.pack(side=pSide, fill=pFill)

    Build menu from menu file
    The menu file may contain commands to set the current directory and to
    set specified environment variables.  This is done while the file is
    being read.  They are not dependent on the selected button.  The last 
    command encountered will be active.

    set environment variable name to value
        ;#set`name=value
    set current directory to path
        ;#cd`path
    set label background color
        ;#label`color
    set button background color
        ;#button`color
    
recipes/Python/577462_A_Buttonbar_program_with_color_/recipe-577462.py