Tim Roberts Wrote:
FT wrote:
The menu text is the HotKey text and there is a built in function for
that as you will see when you go down to the OnMenu handler Call.
Dude, you really need to take a few minutes to establish a source code
···
Subject: Re: [wxpython-users] GetEventObject() /
-
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
Tim, my name is Bruce,
But the mistake was made when converting to DIgest mode and the Subject I
copied from had shortened the actual title.
I was trying to give an example of how to get the menu text item out for
the person and his containers using the object method GetEventObject
I do not even know what you were trying to say...
But, below I shortened it to explain my attempt to give an example and took
out most of my program, which is a working editor with speech for both SAPI
4 and 5 and I am sure even that needs improvements as I learn the code and
the available methods, which prove hard to find, and can not read a book
unless it is in an electronic-text format.
This is what I should have posted:
Subject: Re: [wxpython-users] GetEventObject() /
XXXX
Should have been:
Re: GetEventObject() / wx.Menuitem woes. (Mike Driscoll)
XXXX
From: Mike Driscoll <mike@pythonlibrary.org>
Steve Freedenburg wrote:
let's say I have 2 instances of a button on a frame.
And those 2 buttons are bound to an event.
___Snip___
I looked at how Robin's book did this sort of thing and came up with
def onEvent(self, event):
item = self.GetMenuBar().FindItemById(event.GetId())
text = item.GetText()
wx.MessageBox("You selected the '%s' item" % text)
# Run the program
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MyForm().Show()
app.MainLoop()
</code>
Notice the GetMenuBar() and FindItemById() methods. You'll probably also
like the FindMenuItem() method (which I'm not using) helpful.
-------------------
Mike Driscoll
Hi!
In my case could not find the find method when I wrote mine which is
helpful as Mike pointed out. The other way is to make a copy of all the
menu's and storing
the ID of each. It is better to have the computer assign the id's instead of
making one, but my example does use sequential ID's which could run into
duplication.
I discovered in my example that having all the menu's stored inside a
global list is another approach, but did not even know the find method
existed. In either case the dictionary you want to have located would also
have to use the ID as a key which I assume you are doing to be able to find
that dictionary.
so take a look at my approach and I use the self.menuItems as the
storage place of all the menu's.
My example only stores what is needed for the handler call and what it
needs and that is the text of the menu. Now that text also could be the key
for the dictionary, I guess depending on how you store your dictionaries...
import wx
import os
#import errors2file
import Sapi5, Sapi4
tts5 = Sapi5.Create( {"name":"Mary"})
tts4 = Sapi4.Create( {"name":"Fred"})
#tts = tts5
TS4=4
TS5=5
TTS2 = {TS4:tts4, TS5:tts5}
class MenuParms( object):
def __init__(self, id4item, hk4item, khlp4item, handler4item):
self.id4item = id4item
self.hk4item = hk4item
self.khlp4item = khlp4item
self.handler4item = handler4item
class MainWindow( wx.Frame):
def __init__(self, parent, id, title):
self.ttsp = TS5
self.tts = TTS2[ TS5]
wx.Frame.__init__(self, parent, wx.ID_ANY, title)
#Setting up the menubar.
self.menuBar = wx.MenuBar()
self.menuItems = wx.Menu()
for menuname, items in self.items4menu:
menu = wx.Menu()
for o in items:
menu.Append( o.id4item, o.hk4item, o.khlp4item)
self.menuItems.Append( o.id4item, o.hk4item, o.khlp4item)
menu.AppendSeparator()
wx.EVT_MENU(self, o.id4item, o.handler4item)
wx.EVT_MENU_HIGHLIGHT(self, o.id4item, self.OnMenu)
if menuname == "Settings":
menu.Bind( wx.EVT_CHAR, self.OnKey) #, menu)
self.menuBar.Append( menu, menuname)
# Adding the MenuBar to the Frame content.
self.SetMenuBar( self.menuBar)
# 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)
#TEXT CONTROL KEYS!
def OnMenu(self, event):
"SAY TEXT OF HIGHLIGHTED MENU ITEM!"
event.Skip()
id4btn = event.GetId()
label4btn = self.menuItems.GetLabelText( id4btn)
TTS2[ self.ttsp].Speak( label4btn)
def SetVoice(self,e):
"""SET VOICE PARAMETER ADJUSTED IN SETTINGS HANDLER.
EVENT TRIGGERED BY SLIDER CONTROL INSIDE SETTINGS HANDLER."""
btn = e.GetEventObject()
spin_Id = e.GetId()
value = self.sldr_ctrl.GetValue()
max = self.sldr_ctrl.GetMax()
min = self.sldr_ctrl.GetMin()
label4btn = self.menuItems.GetLabelText( self.menuId)
#NOTE: self.menuId was defined in the settings handler!
TTS2[ self.ttsp].Speak( " %s %s" % (label4btn, str(value)), async,
purge)
if label4btn == "Speaker":
rt = TTS2[ self.ttsp].getRate()
pt = TTS2[ self.ttsp].getPitch()
TTS2[ self.ttsp].setVoice( value)
if self.ttsp==TS5:
TTS2[ self.ttsp].setRate( rt)
TTS2[ self.ttsp].setPitch( pt)
set_value = TTS2[ self.ttsp].getVoiceName()
elif label4btn == "Rate":
TTS2[ self.ttsp].setRate( value)
set_value = str( value)+"%"
elif label4btn == "Pitch":
TTS2[ self.ttsp].setPitch( value)
set_value = str( value)+"%"
elif label4btn == "Volume":
if value < min+5:
TTS2[ self.ttsp].setVolume( 50)
TTS2[ self.ttsp].Speak( "Volume Minimum!")
value = 0
TTS2[ self.ttsp].setVolume( value)
set_value = str( value)+"%"
TTS2[ self.ttsp].Speak(" %s %s" % (label4btn, set_value), async,
purge)
app = wx.PySimpleApp()
frame = MainWindow(None, -1, "Voice Editor")
app.MainLoop()