Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoking a wxPython EVT_BUTTON event programmatically

I've seen another question regarding this topic, but I couldn't quite get the information to work for me, so I thought I'd give my specifics - I suspect I'm just being short-sighted.

I'm trying to exercise my GUI from a test framework, which involves manually invoking an event (in this case a button press) within a test script. So far, in addition to other irrelevant guff, I have:

# In GUI class:

self.button_1 = wx.Button(self, id=wx.ID_ANY, label="Button 1")
self.button_1.Bind(wx.EVT_BUTTON, self.button_1)

# In GUI Test class:

event = wx.PyCommandEvent(X, Y)
wx.PostEvent(get_gui_instance(), event)

My problem is that I don't know what X and Y should be (assuming that the rest is ok). Any help is greatly appreciated.

like image 258
Ed King Avatar asked Mar 31 '26 08:03

Ed King


2 Answers

btnInfo = wx.Button(self,-1,"Some Button")

evt = wx.PyCommandEvent(wx.EVT_BUTTON.typeId,btnInfo.GetId())
wx.PostEvent(self, evt)  #attach event to self ... alternatively maybe attach to btnInfo

should work

like image 83
Joran Beasley Avatar answered Apr 02 '26 21:04

Joran Beasley


So it turns out that because I've re-jigged my GUI to run in a worker thread from GUI Test, I can communicate with it directly. I should have realised this earlier, but nonetheless the result is that I don't need to bother with posting events from GUI Test to GUI since they're running in the same process.

Instead, I can now call the effects of events directly. For example, I can call on_button_press(), bypassing the actual clicking of the button, which would normally fire off the event in wxPython. This allows me to simulate user interaction and test workflows and input ranges, which is exactly what I wanted to do.

Of course, this only works because I'm running my GUI in the same process as the test suite. Posting events seems to be the way forward otherwise, and in answer to my own question, it seems custom events are the way to invoke button presses cross-process. This implies the use of some sort of "test agent" within the GUI to handle those events that are specific for testing.

like image 42
Ed King Avatar answered Apr 02 '26 22:04

Ed King



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!