programmatically triggering a button event

i’m sure this must be possible, but i am having trouble programmatically simulating a button (in this case, a toggle button) click, or event.

this does not work:

evt = wx.CommandEvent(wx.wxEVT_COMMAND_TOGGLEBUTTON_CLICKED)
evt.SetEventObject(self.tcpRelayButton)
evt.SetId(self.tcpRelayButton.GetId())
self.tcpRelayButton.GetEventHandler().ProcessEvent(evt)

any help appreciated,

andrew

Hi Andrew,

Try:

button = self.tcpRelayButton
evt = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, button.GetId())
wx.PostEvent(button, evt)

Regards,

Fahri

···

On Sat, Nov 22, 2008 at 11:39 PM, Andrew Yinger <andrew.yinger@gmail.com> wrote:

i'm sure this must be possible, but i am having trouble programmatically
simulating a button (in this case, a toggle button) click, or event.
this does not work:

  evt = wx.CommandEvent(wx.wxEVT_COMMAND_TOGGLEBUTTON_CLICKED)
  evt.SetEventObject(self.tcpRelayButton)
  evt.SetId(self.tcpRelayButton.GetId())
  self.tcpRelayButton.GetEventHandler().ProcessEvent(evt)
any help appreciated,
andrew
_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

This other solution was tested and works:
def SendEvent(window, PyEventBinder):
# Example of PyEventBinder: wx.EVT_BUTTON
# window is the window (control) that triggers the event
cmd = wx.CommandEvent(PyEventBinder.evtType[0])
cmd.SetEventObject(window)
cmd.SetId(window.GetId())
window.GetEventHandler().ProcessEvent(cmd)
But I don’t use it any more, because somebody suggested me to simply modify the event handler as follows:
def OnToggleButtonPressed(self, event = None):
“”“do something with sense
with a button there is normally no need of event.Skip()”“”
and then,
*when needed, you just call self.*OnToggleButtonPressed()

···

2008/11/23 Fahreddın Basegmez mangabasi@gmail.com

Hi Andrew,

Try:

button = self.tcpRelayButton

evt = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, button.GetId())

wx.PostEvent(button, evt)

Regards,

Fahri

On Sat, Nov 22, 2008 at 11:39 PM, Andrew Yinger andrew.yinger@gmail.com wrote:

i’m sure this must be possible, but i am having trouble programmatically

simulating a button (in this case, a toggle button) click, or event.

this does not work:

evt = wx.CommandEvent(wx.wxEVT_COMMAND_TOGGLEBUTTON_CLICKED)

evt.SetEventObject(self.tcpRelayButton)

evt.SetId(self.tcpRelayButton.GetId())

self.tcpRelayButton.GetEventHandler().ProcessEvent(evt)

any help appreciated,

andrew


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

It looks like none of the solutions posted actually give the visual appearance of the button being clicked -- they just invoke the same code the button normally invokes.

But for some purposes, you want to show the user that whatever they did (e.g., hit the Esc key) was equivalent to clicking on a button (e.g. the Cancel button), and at least on Mac OS X, the "proper" way of doing that is to actually draw the button depressed and then released.

How would one do that in wx?

Thanks,
- Joe

yes, that is my problem – all of the above ‘work’ in the sense that they ultimately call the same event-handling code as if the button was pressed, but (and this is especially evident in the case of the toggle button) i can’t figure out how to programmatically trigger the visual appearance.

  • andrew
···

On Sun, Nov 23, 2008 at 8:18 AM, Joe Strout joe@strout.net wrote:

It looks like none of the solutions posted actually give the visual appearance of the button being clicked – they just invoke the same code the button normally invokes.

But for some purposes, you want to show the user that whatever they did (e.g., hit the Esc key) was equivalent to clicking on a button (e.g. the Cancel button), and at least on Mac OS X, the “proper” way of doing that is to actually draw the button depressed and then released.

How would one do that in wx?

Thanks,

  • Joe

wxpython-users mailing list
wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Could you show the code that creates your toggle button?

···

On Sun, Nov 23, 2008 at 9:49 AM, Andrew Yinger <andrew.yinger@gmail.com> wrote:

yes, that is my problem -- all of the above 'work' in the sense that they
ultimately call the same event-handling code as if the button was pressed,
but (and this is especially evident in the case of the toggle button) i
can't figure out how to programmatically trigger the visual appearance.

- andrew

On Sun, Nov 23, 2008 at 8:18 AM, Joe Strout <joe@strout.net> wrote:

It looks like none of the solutions posted actually give the visual
appearance of the button being clicked -- they just invoke the same code the
button normally invokes.

But for some purposes, you want to show the user that whatever they did
(e.g., hit the Esc key) was equivalent to clicking on a button (e.g. the
Cancel button), and at least on Mac OS X, the "proper" way of doing that is
to actually draw the button depressed and then released.

How would one do that in wx?

Thanks,
- Joe

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

self.tcpRelayButton = wx.ToggleButton(controls, -1, label=‘TCP Relay’)
self.Bind(wx.EVT_TOGGLEBUTTON, self.onTCPRelay, self.tcpRelayButton)

… and i do this to trigger the event programmatically (again, with no visible change in UI):

self.tcpRelayButton.Command(wx.PyCommandEvent(wx.EVT_TOGGLEBUTTON.evtType[0], self.tcpRelayButton.GetId()))

···

On Sun, Nov 23, 2008 at 9:04 AM, Fahreddın Basegmez mangabasi@gmail.com wrote:

On Sun, Nov 23, 2008 at 9:49 AM, Andrew Yinger andrew.yinger@gmail.com wrote:

yes, that is my problem – all of the above ‘work’ in the sense that they

ultimately call the same event-handling code as if the button was pressed,
but (and this is especially evident in the case of the toggle button) i
can’t figure out how to programmatically trigger the visual appearance.

  • andrew

On Sun, Nov 23, 2008 at 8:18 AM, Joe Strout joe@strout.net wrote:

It looks like none of the solutions posted actually give the visual

appearance of the button being clicked – they just invoke the same code the
button normally invokes.

But for some purposes, you want to show the user that whatever they did

(e.g., hit the Esc key) was equivalent to clicking on a button (e.g. the
Cancel button), and at least on Mac OS X, the “proper” way of doing that is
to actually draw the button depressed and then released.

How would one do that in wx?

Thanks,

  • Joe

wxpython-users mailing list

wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users


wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Could you show the code that creates your toggle button?

wx.ToggleButton.SetValue(True or False) triggers the button appearance, and then the event handler must be called.

···

2008/11/23 Andrew Yinger andrew.yinger@gmail.com

yes, that is my problem – all of the above ‘work’ in the sense that they ultimately call the same event-handling code as if the button was pressed, but (and this is especially evident in the case of the toggle button) i can’t figure out how to programmatically trigger the visual appearance.

  • andrew

On Sun, Nov 23, 2008 at 8:18 AM, Joe Strout joe@strout.net wrote:

It looks like none of the solutions posted actually give the visual appearance of the button being clicked – they just invoke the same code the button normally invokes.

But for some purposes, you want to show the user that whatever they did (e.g., hit the Esc key) was equivalent to clicking on a button (e.g. the Cancel button), and at least on Mac OS X, the “proper” way of doing that is to actually draw the button depressed and then released.

How would one do that in wx?

Thanks,

  • Joe

wxpython-users mailing list
wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Joe Strout wrote:

It looks like none of the solutions posted actually give the visual appearance of the button being clicked -- they just invoke the same code the button normally invokes.

wx does not support turning arbitrary wx.Events into native system events/messages/whatever and sending them to the native widget as if they had happened naturally. There are just too many variations (even on different versions of the same platform) to be able to do that reliably.

But for some purposes, you want to show the user that whatever they did (e.g., hit the Esc key) was equivalent to clicking on a button (e.g. the Cancel button), and at least on Mac OS X, the "proper" way of doing that is to actually draw the button depressed and then released.

How would one do that in wx?

There is the wx.RendererNative class that allows you to use the native theme to draw various control components to a DC, but they would typically only be used when creating a generic widget that is intended to look like a native widget, not for drawing a real widget in some other state... Probably the only way to do it is to use whatever native APIs are available to tell the widget to draw itself differently, but that is outside of the scope of wx.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

I don't think that's what is needed, though. A button just needs a "Push" method that draws it in the depressed state, waits a fraction of a second, and then redraws it in its normal state, before invoking the normal wx event handler. System events aren't involved.

I know this is possible cross-platform because REALbasic manages it (on all the same platforms that wx runs on, I believe).

Of course, wx does many things that RB does not do, so I'm not denigrating one or the other. It's just a limitation to be aware of, and maybe someday I'll be versed enough in wx to roll up my sleeves and contribute this feature myself.

Thanks,
- Joe

···

On Dec 2, 2008, at 12:20 AM, Robin Dunn wrote:

It looks like none of the solutions posted actually give the visual appearance of the button being clicked -- they just invoke the same code the button normally invokes.

wx does not support turning arbitrary wx.Events into native system events/messages/whatever and sending them to the native widget as if they had happened naturally. There are just too many variations (even on different versions of the same platform) to be able to do that reliably.

Joe Strout wrote:

I know this is possible cross-platform because REALbasic manages it (on all the same platforms that wx runs on, I believe).

but does it use native widgets, like wx, or does it draw its own?

> A button just needs a
> "Push" method that draws it in the depressed state, waits a fraction of
> a second, and then redraws it in its normal state, before invoking the
> normal wx event handler.

"just" -- if the native widgets don't have such a method, then how do you create it?

It's just a limitation to be aware of,

correct -- there are a lot of limitations to wrapping the native widgets, rather than drawing them yourself. I sometimes wonder if it's worth it, but then I go and use a QT or GTK based app on a Mac and come running back, grateful, to wx.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

I know this is possible cross-platform because REALbasic manages it (on all the same platforms that wx runs on, I believe).

but does it use native widgets, like wx, or does it draw its own?

It uses native widgets.

> A button just needs a
> "Push" method that draws it in the depressed state, waits a fraction of
> a second, and then redraws it in its normal state, before invoking the
> normal wx event handler.

"just" -- if the native widgets don't have such a method, then how do you create it?

You want me to dig into the REALbasic source? I'm not legally permitted to do that any more. :slight_smile: But you don't need a method that does all of the above; you create it by calling a method that draws the native widget in a "pushed" state (or, actually changes its state to that), then waiting a bit (using a busy loop if necessary -- we're only talking about a fraction of a second), and then drawing it again in its normal state.

It's just a limitation to be aware of,

correct -- there are a lot of limitations to wrapping the native widgets, rather than drawing them yourself.

Sure, but that excuse doesn't apply here.

I sometimes wonder if it's worth it, but then I go and use a QT or GTK based app on a Mac and come running back, grateful, to wx.

Hah, too true!

Best,
- Joe

···

On Dec 2, 2008, at 12:30 PM, Christopher Barker wrote:

Joe Strout wrote:

You want me to dig into the REALbasic source?

Well, actually, I asked how YOU would do, not how they did it, but if you have the source.....

But you don't need a method that does all of the above; you create it by calling a method that draws the native widget in a "pushed" state

right, but does such a method exist for all the native widgets? I have no idea.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov