Programmatically clicking button wxpython

303 views
Skip to first unread message

Adi Redzic

unread,
Apr 15, 2018, 5:28:03 PM4/15/18
to wxPython-users

Been trying to figure this one out for a while. I am trying to click a button (wx.button) in my wx.Frame to move the selection of a Object List View (OLV) with self_list._SelectAndFocus(i). The button is clicked from a MCU and i can see the click going though.

Tried using this code

evt = wx.PyCommandEvent(wx.EVT_BUTTON.typeId, self.button3.GetId())
wx.PostEvent(self.button3, evt)

The event goes through but the ObjectListView is not responding. Clicking another button with the mouse in wxpython works just fine. Seems to me that the PostEvent is not going through like a regular mouse click on a button.

I also tested it with:

self.uisim = wx.UIActionSimulator()
self.uisim.MouseMove(1200,650)
self.uisim.MouseClick(1)

Forcing a mouse click on the respected button and this works. I fear there is a limitation to the OLV, any tips?


It might also be that i am running multiple threads with


tester = Atem()
main = MainFrame(self)
thread = Thread(target=tester.RunCode)
thread2 = Thread(target=main.cutmonitor)
# thread3 = Thread(target=main.test2)
thread.daemon = True
thread2.daemon = True
# thread3.daemon = True
thread.start()
thread2.start()


tester.RunCode is where i start the MCU loop thread and main.cutmonitor is where i get the data from the button of the MCU. So cutmonitor is monitoring the button on the MCU and every time this button is pressed self_list._SelectAndFocus(i) is iterating with 1. i=1, i=2 and so on.

row_items = self.list.GetItemCount()
In a Mainloop this return the correct number of Items in the OLV
But if i make a new function etc 
def row_item(self, event):
        row_items = self.cam_list.GetItemCount()

it returns 0. 


Tim Roberts

unread,
Apr 16, 2018, 2:58:20 AM4/16/18
to wxpytho...@googlegroups.com
On Apr 15, 2018, at 7:52 AM, Adi Redzic <adire...@gmail.com> wrote:

Been trying to figure this one out for a while. I am trying to click a button (wx.button) in my wx.Frame to move the selection of a Object List View (OLV) with self_list._SelectAndFocus(i). The button is clicked from a MCU and i can see the click going though.

Tried using this code

evt = wx.PyCommandEvent(wx.EVT_BUTTON.typeId, self.button3.GetId())
wx.PostEvent(self.button3, evt)

The event goes through but the ObjectListView is not responding. Clicking another button with the mouse in wxpython works just fine. Seems to me that the PostEvent is not going through like a regular mouse click on a button.

In general, this is not the right way to do this kind of thing.  Instead, you should have a separate function that takes the action that button3 does.  Then, the button3 click handler should call that action, and where you need to execute this action from elsewhere should call the action.  It's not a good design to trigger actions within a single app by simulating clicks and menu hits.

It might also be that i am running multiple threads with

tester.RunCode is where i start the MCU loop thread and main.cutmonitor is where i get the data from the button of the MCU. So cutmonitor is monitoring the button on the MCU and every time this button is pressed self_list._SelectAndFocus(i) is iterating with 1. i=1, i=2 and so on.

It is never a good idea to refer to any UI elements outside the thread where they were created.  Sometimes, on some operating systems, things seem to work, but the contract is that all UI-related activity must take place on the window's original thread.  wx.CallAfter will be your friend here. 
— 
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Adi Redzic

unread,
May 4, 2018, 7:10:05 AM5/4/18
to wxpytho...@googlegroups.com
Hei Tim

Thanks for the reply, i have not got the chance to look at this yet. But going through the documentation of wx.CallAfter seems to fit what i am looking for. I will come back to you when i have tested this.  :)

Btw. Does anyone know if it is possible to change the color of the marking when you click to edit a cell? :)

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Adi Red
zic
+47 474 41 494

Reply all
Reply to author
Forward
0 new messages