psychopy.org | Reference | Downloads | Github

Gui controls with callback functions

Hello, sorry if that topic has been discussed before, i could not find anything relevant. I need to have some graphical widgets to control calibration parameters while the main Psychopy script is running. I am somewhat familiar with Qt and I think I can implement such widgets using it, but I am not sure how is it possible to integrate its signals and slots messaging into Psychopy’s event loop. I understand that Psychopy has its own event loop and if I used gui widgets not supported by Psychopy I need a separate event loop to run them. Is this correct?

Basically, I need to change some variable values manually, not programmatically, whilst the Psychopy main loop is already running.

Hi - because psychopy runs in full screen mode, any calls to QT occur behind the main window - which is why dialogue boxes can’t be used. At a guess, one way around this could be to set up a second monitor and have all QT instances set to win2 rather than the primary monitor

Hi Oli,
Thank you for your reply! In fact my setup has two monitors already, one for the presentation of visual stimuli and another 1 for control of the experiments. My main issue in the following: when I ran Qt instances I do it from: if name == “main”:
app = QApplication(sys.argv)

module. This starts the event loop of the QApplication.
However, I ran Psychopy differently from its own gui as a script and as I understand, Psychopy starts its own event loop. I want to implement additional controls with Qt widgets that need to callback Psychopy objects and change such attributes as position and size at runtime of the Psychopy script. But then from my understanding there would be two event loops running in parallel and I cannot wrap my head around this problem. Of course please correct me if my understanding in not correct.

I don’t personally know of a way to send events to psychopy from Qt, but it’s got me thinking about workarounds.

How complex does your gui need to be, and have you seen this post by Jon about full screen mode and the gui dialog? I would think the easiest way to go would be to use the existing psychopy.gui module. I find the drop-down menus to be quite useful. I also wonder if one could modify psychopy.gui to incorporate other qt widgets if you need them, but it’s just a thought, I haven’t looked into it. If you really want to expand integration with Qt, you should study the file where that happens: psychopy/event/qtgui.py , in the GitHub repository, for example.

Another option is just to program the gui and events using psychopy’s existing tools, but this depends on how complicated it has to be. For an experiment I’m running now I’d made some buttons and drag and drop features, but it certainly took a little elbow grease.

I think @daniel.riggs1 is on the right track:if your GUI needs aren’t too complicated, try to keep everything within PsychoPy. You will be heading for a world of pain by trying to have separate processes with interfaces.

I have previously run a second window for the experimenter, displaying a rating scale so the experimenter can change settings on the fly. This might be even more convenient than using the dialog box, as you can respond to changes immediately and continuously, rather than waiting for the dialog box to open and then return.

Thank you so much for detailed replies!

Unfortunately, I need a quite complex control center for an experiment, ideally with easy controls a la knobs and sliders and Matplotlib real-time graphing canvas at the center. For now I have implementedthe real-time plotting gui as a separate python process that communicates with Psychopy via a local websocket, but I can only read out the data that Psychopy receives from the eye tracker in this setup, not callback Psychopy and modify the running script.
So I need something that looks like this:


I work with small primate species: common marmosets. therefore I cannot calibrate the eye-tracker by standard methods and can only work with raw data that I need to constantly fine-tune in real time to achieve good calibration.
I will study qtgui.py file. Before it I wanted to see if someone else has already been trying to do something similar to my plan. I will gladly share the code if I manage to make something useful.
I hope Jon can find the time to give me some pointers as well.

Wow, sounds like a cool experiment and you clearly know what you are doing technically. I’m a bit lost, though, as to why the communication can’t be two-way. Surely the PsychoPy script can just periodically check a web socket/UDP port or whatever for input and modify variables accordingly?

I am just afraid the dependence on external input from a separate process can mess up the timing of my main loop inside Psychopy. For gui plotting the listener function runs in its own thread, but threads in Python are not truly parallel and share the same interpreter. For realt-time plotting i use a Python 3 interpreter to separate processes.