#----------------------------------------------------------------------------- from Tkinter import * import tkFileDialog import string import os global FullFileName FullFileName='' #----------------------------------------------------------------------------- HamPhon={ 'A':'Alpha', 'B':'Bravo', 'C':'Charlie', 'D':'Delta', 'E':'Echo', 'F':'Foxtrot', 'G':'Golf', 'H':'Hotel', 'I':'India', 'J':'Juliet', 'K':'Kilo', 'L':'Lima', 'M':'Mike', 'N':'November', 'O':'Oscah', 'P':'Papa', 'Q':'Quebec', 'R':'Romeo', 'S':'Sierra', 'T':'Tango', 'U':'Uniform', 'V':'Victor', 'W':'Whiskey', 'X':'X-ray', 'Y':'Yankee', 'Z':'Zulu'} #----------------------------------------------------------------------------- CopPhon={ 'A':'Adam', 'B':'Boy', 'C':'Charles', 'D':'David', 'E':'Edward', 'F':'Frank', 'G':'George', 'H':'Henry', 'I':'Ida', 'J':'John', 'K':'King', 'L':'Lincoln', 'M':'Mary', 'N':'Nora', 'O':'Ocean', 'P':'Paul', 'Q':'Queen', 'R':'Robert', 'S':'Sam', 'T':'Tom', 'U':'Union', 'V':'Victor', 'W':'William', 'X':'X-ray', 'Y':'Young', 'Z':'Zebra'} #----------------------------------------------------------------------------- OldMilPhon={ 'A':'Able', 'B':'Baker', 'C':'Charlie', 'D':'Dog', 'E':'Easy', 'F':'Fox', 'G':'George', 'H':'How', 'I':'Item', 'J':'Jig', 'K':'King', 'L':'Love', 'M':'Mike', 'N':'Nan', 'O':'Oboe', 'P':'Peter', 'Q':'Queen', 'R':'Roger', 'S':'Sugar', 'T':'Tare', 'U':'Uncle', 'V':'Victor', 'W':'William', 'X':'X-ray', 'Y':'Yoke', 'Z':'Zebra'} #----------------------------------------------------------------------------- PolistraPhon={ 'A':'aye', 'B':'brew', 'C':'cher', 'D':'dzus', 'E':'eye', 'F':'few', 'G':'gnu', 'H':'high', 'I':'I', 'J':'juice', 'K':'knew', 'L':'loo', 'M':'moo', 'N':'new', 'O':'ooze', 'P':'phew', 'Q':'quay', 'R':'rue', 'S':'share', 'T':'thew', 'U':'uzi', 'V':'view', 'W':'why', 'X':'xerox', 'Y':'youse', 'Z':'zeus'} #----------------------------------------------------------------------------- # Next section is TK #------------------------------------------------------------ class App: def __init__(self, master, textMessage): self.master = master master.title("Phonetic alphabet") self.StatusEntry = Entry(self.master,width=40) self.StatusEntry.grid(row=0,column=0,columnspan=4) self.StatusEntry.insert(0,"Hit a button to select file to convert.") # Go/nogo buttons. self.buttonHam = Button(self.master, text="Ham", command=self.HandleHam) self.buttonHam.grid(row=3, column=0) self.buttonCop = Button(self.master, text="Cop", command=self.HandleCop) self.buttonCop.grid(row=3, column=1) self.buttonCop = Button(self.master, text="Mil", command=self.HandleMil) self.buttonCop.grid(row=3, column=2) self.buttonPol = Button(self.master, text="New", command=self.HandlePol) self.buttonPol.grid(row=3, column=3) self.master.protocol('WM_DELETE_WINDOW', self.HandleCancel) # - - - - - - - - - - - - - - - - - - def ShowStatus(self,S): # Just saving repetition self.StatusEntry.delete(0,END) self.StatusEntry.insert(0,S) self.StatusEntry.update_idletasks() # - - - - - - - - - - - - - - - - - - def HandleName(self): global FullFileName # Pop up file box to get name. fn=tkFileDialog.askopenfilename(defaultextension=[("File with source text", "*.TXT *.txt")] ) if not fn: self.ShowStatus("No file selected. Try again.") return 0 FullFileName=os.path.abspath(fn) # - - - - - - - - - - - - - - - - - - def WritePhon(self,Phon): global FullFileName fpin=open(FullFileName,'rt') st=fpin.read() fpin.close() outst='' for c in st: upc=string.upper(c) if upc in string.ascii_uppercase: outst=outst+Phon[upc] outst=outst+' ' else: outst=outst+c fpout=open('x.txt','wt') fpout.write(outst) fpout.close() # - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - def HandleHam(self): # Action here self.HandleName() self.WritePhon(HamPhon) self.master.destroy() # - - - - - - - - - - - - - - - - - - def HandleMil(self): # Action here self.HandleName() self.WritePhon(OldMilPhon) self.master.destroy() # - - - - - - - - - - - - - - - - - - def HandleCop(self): # Action here self.HandleName() self.WritePhon(CopPhon) self.master.destroy() # - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - def HandlePol(self): # Action here self.HandleName() self.WritePhon(PolistraPhon) self.master.destroy() # - - - - - - - - - - - - - - - - - - def HandleCancel(self): self.master.destroy() #------------------------------------------------------------ # Activate the loop #------------------------------------------------------------ root = Tk() app = App(root, '') root.mainloop() #------------------------------------------------------------ # End TK loop. #-----------------------------------------------------------------------------