How to manipulate my GUI using extrnal commands

Dear Sirs,
lately I started to work, for the first time, on a new project using the Raspberry Pi 4, Python and wxPython. I have started to create my GUI that will be controlled by a touch screen and some signals generated by some toggle switches and sensors that all are interfaced with my Pi GPIO. I took a look at wxPython API documentations to understand which event I should bind to my handler and I saw that I could use a custom events but I still do not understand how I can create them and link them to my external inputs.

For example : if my GPIO.input(15) is True, I get a change on my GUI (a change of the background colour) and if it is False I get my initial GUI.

Looking forward to receiving Your comments,

Yours faithfully,

Mohamed

Well, you have to understand that your wxPython GUI runs in its own process, forever spinning its main loop. So, if you have other processes doing their own jobs in the meantime, we are basically talking inter-process communication here. Feel free to apply any technique that best suits your case. Have the other process send data over a socket, or use a pipe or… whatever.
From the gui process’ side, all you have to do is to listen to your socket/pipe/whatever from time to time, and retrieve the data. You can set up a wx.Timer and the catch the wx.EVT_TIMER event, if you need regular intervals. Or, you can just catch the irregular wx.EVT_IDLE (browse the docs for wx.IdleEvent), but only if you foresee that the gui will be, in fact, manipulated by an actual user (an IdleEvent won’t work if the gui is left untouched for a while).

Hi, mohamedh

The external commands sound like communication from the outside processes such as socket, pipe, REST, COM, multiprocessing … and so on.

As @ricpol pointed out, you can do everything within the wxpython process using wx.Timer

import wx

class Frame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        
        self.timer = wx.Timer(self)
        self.timer.Start(1000)
        self.Bind(wx.EVT_TIMER, self.OnTimer)

    def OnTimer(self, evt):
        if GPIO.input(15):
            print(evt)
            ...
        evt.Skip()

if __name__ == "__main__":
    app = wx.App()
    frm = Frame(None)
    frm.Show()
    app.MainLoop()

Hi @ricpol, @komoto48g,
it’s working perfectly!!:D, I’m really grateful!!
Thank you for your reply and your efforts!!!

Mohamed