desc="""In the following example we use a sizer with vertical layout""" #Editor.py import wx import os import Sapi5 tts = Sapi5.Create( {"name":"Mary"}) purge = tts._purge async = tts._async punc = tts._punc ID=0 HK=1 KH=2 MK=3 MF=4 MF2=5 DW_ID=1000 class MainWindow(wx.Frame): def __init__(self, parent, id, title): self.dirname=os.getcwd() #SEARCH FROM PRESENT DIRECTORY! self.filename="" self.items4menu = {"File": [ {ID:102, HK:"&Open", KH:" Open a file to edit", MF:self.OnOpen}, {ID:103, HK:"&Save", KH:" save file to disk", MF:self.OnSave}, {ID:104, HK:"&Edit", KH:" Do editing", MF:self.OnEdit}, {ID:101, HK:"&About", KH:" Information about this program", MF:self.OnAbout}, {ID:109, HK:"E&xit", KH:" Terminate the program", MF:self.OnExit} ], #END OF FILE MENU! "Voice": [ {ID:202, HK:"&Read", KH:" Open a file to read", MF:self.OnWav2Read}, {ID:203, HK:"&Save", KH:" save text to audio file", MF:self.OnSave2Wav}, {ID:204, HK:"Te&xt", KH:" read text field", MF:self.OnRead}, {ID:205, HK:"&Quit", KH:" Stop Reading", MF:self.OnQuitRead} ], #END OF VOICE MENU! "Settings": [ {ID:302, HK:"&Speaker", KH:" Name for voice.", MF:self.OnEnter, MF2:self.OnVoice}, {ID:303, HK:"&Rate", KH:" Rate for voice.", MF:self.OnEnter, MF2:self.OnVoice}, {ID:304, HK:"&Pitch", KH:" Pitch for voice.", MF:self.OnEnter, MF2:self.OnVoice}, {ID:305, HK:"&Volume", KH:" Volume for voice.", MF:self.OnEnter, MF2:self.OnVoice} ], #END OF SETTINGS MENU! "Down": [ {ID:DW_ID, HK:"&Down", KH:" Lower Setting.", MF:self.OnEnter, MF2:self.OnVoice} ] #END OF DOWN MENU! } #END OF ITEMS FOR MENU! wx.Frame.__init__(self, parent, wx.ID_ANY, title) self.control = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE) self.control.Bind( wx.EVT_KEY_UP, self.OnKey_Up) #, self.control) # self.control.Bind( wx.EVT_CHAR, self.OnKey) #, self.control) self.CreateStatusBar() #A Statusbar in the bottom of the window #Setting up the menu. filemenu = wx.Menu() for o in self.items4menu["File"]: filemenu.Append( o[ID], o[HK], o[KH]) filemenu.AppendSeparator() voicemenu = wx.Menu() for o in self.items4menu["Voice"]: voicemenu.Append( o[ID], o[HK], o[KH]) voicemenu.AppendSeparator() self.setting_menu = wx.Menu() for o in self.items4menu["Settings"]: down_menu = wx.Menu() down_menu.Append( o[ID], o[HK], o[KH]) d = self.items4menu["Down"][0] down_menu.Append( d[ID]+o[ID], d[HK], d[KH]) self.setting_menu.AppendMenu( o[ID], o[HK], down_menu) self.setting_menu.AppendSeparator() voicemenu.AppendMenu(-1, "&VoiceSettings", self.setting_menu) # Creating the menubar. menuBar = wx.MenuBar() menuBar.Append( filemenu,"&File") # Adding the "filemenu" to the MenuBar menuBar.Append( voicemenu,"&Voice") # Adding the "voicemenu" to the MenuBar self.SetMenuBar( menuBar) # Adding the MenuBar to the Frame content. self.data4menu = {} for o in self.items4menu["File"]: wx.EVT_MENU(self, o[ID], o[MF]) self.data4menu[ o[ID]] = o for o in self.items4menu["Voice"]: wx.EVT_MENU(self, o[ID], o[MF]) self.data4menu[ o[ID]] = o for o in self.items4menu["Settings"]: wx.EVT_MENU(self, o[ID], o[MF2]) self.data4menu[ o[ID]] = o wx.EVT_MENU(self, o[ID]+DW_ID, o[MF2]) self.data4menu[ o[ID]+DW_ID] = o # Use some sizers to see layout options self.sizer=wx.BoxSizer(wx.VERTICAL) self.sizer.Add(self.control,1,wx.EXPAND) #Layout sizers self.SetSizer(self.sizer) self.SetAutoLayout(1) self.sizer.Fit(self) self.Show(1) #SAY LABEL OF MENU ITEM! def OnEnter(self, event): "WHEN ENTERING SAY LABEL OF BUTTON!" # self.button2bind = self.Bind(wx.EVT_BUTTON, self.OnVoice, id=event.GetId()) eventType = event.GetEventType() # tts.Speak( event.GetPosition()) label4btn = self.setting_menu.GetLabelText( event.GetId()) tts.Speak( label4btn, async, punc) set_value = " Error In Label!" if label4btn == "Down": set_value = str(-1) if label4btn == "Speaker": set_value = tts.getVoiceName() elif label4btn == "Rate": set_value = str( tts.getRate()) elif label4btn == "Pitch": set_value = str( tts.getPitch()) elif label4btn == "Volume": set_value = str( tts.getVolume()) + "%" text = label4btn +" Button " +set_value tts.Speak( text, async) #TEXT CONTROL KEYS! def OnKey(self, event): "KEY CAPTURE FOR EDITING! MUST USE EVT_CHAR FOR ALL CODES." k = event.GetKeyCode() m = event.GetModifiers() txt = self.control.GetValue() event.Skip() #comment rk = event.GetRawKeyCode() #not in all platforms #comment uk = event.GetUnicodeKey() #Not on platforms that unicode was not installed!#comment #comment tts.Speak("%d %d" % (k,m)) #comment x2y4m = event.GetPosition() #Mouse position in event window, textctrl! c = self.control.GetInsertionPoint() #present line column position when event fired! tl = self.control.GetLastPosition() #last point in textctrl colmax, linemax = self.control.PositionToXY( tl) col, line = self.control.PositionToXY( c) lx = self.control.GetLineLength( line) #length of line specified! if k==wx.WXK_LEFT: col-=1 if col < 0: col = 0 line-=1 if line < 0: line=0 else: col = self.control.GetLineLength( line); lx=col if k==wx.WXK_RIGHT: col+=1 if col > lx: col = 0 line+=1 if line > linemax: line=linemax; col=colmax if k==wx.WXK_UP: line-=1 if line<0: line=0 lx = self.control.GetLineLength( line) #length of line specified! if col>lx: col=lx if k==wx.WXK_DOWN: line+=1 if line>linemax: line=linemax lx = self.control.GetLineLength( line) #length of line specified! if col>lx: col=lx c = self.control.XYToPosition( col, line) cs = self.control.XYToPosition( 0, line) ce = self.control.XYToPosition( lx, line) if c<0: c=0 if c>tl: c=tl lxy = self.control.PositionToXY( c) #comment tts.Speak( "Col %d Line %d" % lxy, async, purge) #comment col, line = lxy lx = self.control.GetLineLength( line) #length of line specified! #comment tts.Speak( " line %d col %d length %d" % (line, col, lx), async, purge) #comment tts.Speak( self.control.HitTest( lxy)) #mouse char pixels using coordinate values ch = self.control.GetRange( c,c+1) if ch and ord(ch)==10: ch="Return" elif ch and ord(ch)==32: ch="space" if k in [wx.WXK_UP, wx.WXK_DOWN]: ch = self.control.GetRange( cs, ce) tts.Speak( " %s " % ch, async, purge) #comment if k==wx.WXK_LEFT and ord(ch)==10: c-=1 #comment if k==wx.WXK_RIGHT and ord(ch)==10: c+=1 #comment tts.Speak(" %s %d %d" % (ch, c, ord(ch)), async) #KEY UP OR KEY RELEASED! def OnKey_Up(self, event): "SAY CHAR OR LINE ON WHEN KEY RELEASED!" event.Skip() k = event.GetKeyCode() m = event.GetModifiers() txt = self.control.GetValue() c = self.control.GetInsertionPoint() #present line column position when event fired! tl = self.control.GetLastPosition() #last point in textctrl colmax, linemax = self.control.PositionToXY( tl) col, line = self.control.PositionToXY( c) lx = self.control.GetLineLength( line) #length of line specified! #comment c = self.control.XYToPosition( col, line) #CURSOR POSITION! cs = self.control.XYToPosition( 0, line) #LINE START! ce = self.control.XYToPosition( lx, line) #LINE END! ch = self.control.GetRange( c,c+1) if ch: if ord(ch)==10: ch="Return" elif ord(ch)==wx.WXK_SPACE: ch="space" elif c==tl and k in [wx.WXK_RIGHT, wx.WXK_END]: ch="end of text" if c= tts.getVoiceCount(): value = tts.getVoiceCount()-1 tts.setVoice( value) set_value = tts.getVoiceName() elif label4btn == "Rate": value = tts.getRate() value += dir if value > 10: value = 10 if value < -10: value = -10 tts.setRate( value) set_value = str( value) elif label4btn == "Pitch": value = tts.getPitch() value += dir if value > 10: value = 10 if value < -10: value = -10 tts.setPitch( value) set_value = str( value) elif label4btn == "Volume": value = tts.getVolume() value += 10*dir if value > 100: value = 100 if value < 0: tts.setVolume( 50) tts.Speak( "Volume Minimum!") value = 0 tts.setVolume( value) set_value = str( value)+"%" tts.Speak( set_value, async) def OnAbout(self,e): "A dialog box saying what the editor is about!" text = " A sample editor with voice \n in wxPython." tts.Speak( "About A Sample Editor!"+text) d= wx.MessageDialog( self, text, "About Sample Editor", wx.OK) # Create a message dialog box d.ShowModal() # Shows it d.Destroy() # finally destroy it when finished. def OnExit(self,e): self.Close(True) # Close the frame. def OnOpen(self,e): """ Open a file""" self.setFilePath( "o") f=open(os.path.join(self.dirname, self.filename),'r') self.control.SetValue(f.read()) f.close() def OnSave(self,e): """ Save a file""" self.setFilePath( "s") f=open(os.path.join(self.dirname, self.filename),'w') # self.control.SetValue(f.read()) f.write( self.control.GetValue()) f.close() def setFilePath(self, type4file=""): " Search directory for path and file name!" fn=self.filename t4f = wx.OPEN if type4file[0] in "sS": t4f = wx.SAVE|wx.FD_OVERWRITE_PROMPT # fn = self.filename dlg = wx.FileDialog(self, self.filename +" or Choose a file", self.dirname, fn, "*.*", t4f) if dlg.ShowModal() == wx.ID_OK: self.filename = dlg.GetFilename() self.dirname = dlg.GetDirectory() dlg.Destroy() def OnWav2Read(self,e): """ Open a file to read""" self.setFilePath( "o") tts.SpeakFromWav( os.path.join(self.dirname, self.filename), async, purge) def OnSave2Wav(self,e): """ Save text to a wav file""" self.setFilePath( "s") tts.SpeakToWav( os.path.join(self.dirname, self.filename), self.control.GetValue()) def OnRead(self,e): """ Read the text""" tts.Speak( self.control.GetValue(), async, purge) def OnQuitRead(self,e): """ Quit the reading of the text""" tts.Speak( " Talking Stopped!", purge) def OnEdit(self,e): """ Edit the file""" self.control.SetFocus() app = wx.PySimpleApp() frame = MainWindow(None, -1, "Sample editor") app.MainLoop() desc2="""The sizer.Add method has three arguments. The first one specifies the control to include in the sizer. The second one is a weight factor which means that this control will be sized in proportion to other ones. For example, if you had three edit controls and you wanted them to have the proportions 3:2:1 then you would specify these factors as arguments when adding the controls. 0 means that this control or sizer will not grow. The third argument is normally wxGROW (same as wxEXPAND ) which means the control will be resized when necessary. If you use wxSHAPED instead, the controls aspect ratio will remain the same. If the second parameter is 0, i.e. the control will not be resized, the third parameter may indicate if the control should be centered horizontally and/or vertically by using wxALIGN_CENTER_HORIZONTAL, wxALIGN_CENTER_VERTICAL, or wxALIGN_CENTER (for both) instead of wxGROW or wxSHAPED as that third parameter. You can alternatively specify combinations of wxALIGN_LEFT, wxALIGN_TOP, wxALIGN_RIGHT, and wxALIGN_BOTTOM. The default behavior is equivalent to wxALIGN_LEFT | wxALIGN_TOP."""