0
votes

I'm working on revising an old Java GUI to python and have decided to try to create and implement this in PyQt4.

The original program connects to an outside server and uses the data set by the server to update various figures. These figures are opened from a main GUI and operate and update independently of each other.

I have managed to set something similar up that opens a main GUI window, and connects to an outside server. Where I am having an issue is updating a figure when I get new data from the server (I figured I'd start with one figure then increase from there).

Due to my lack of understanding, or python documentation found, on setting up a client to server connection or threading in the GUI through PyQt I have implemented various work arounds. I connect to the server and read in data via a twisted client. This client is run on a separate thread implemented from the python threading module. This at least from what I can tell works.

I can open up the figures with the newly updated data from the main window but I can not seem to update them from the twisted client thread. I get errors like:

QObject::setParent: Cannot set parent, new parent is in a different thread or exceptions.RuntimeError: main thread is not in main loop

From my research I understand that for PyQt windows must be updated from the main thread. I am not sure how to do this. My current implementation has the main thread carrying the main initial window and I am unsure of how to trigger it to update the child windows when new data is received.

Any ideas or links that might give me either an idea how to implement this or what could be a better framework or setup of the GUI?

EDIT: The current set up of my code creates the thread that sets up the client connection and processes the server messages is currently set to a Daemon thread. Since, it does not have an exit condition unless the connection to the server is lost. The Twisted client does not allow the user to return data but since the data I need to update the figures is stored inside of the self variable they are updated during each new data packet. All examples of QTread seem to have an exit condition how would I get it to work without one? Or how do I set up my client side to have an exit statement and then reconnect to get the next message?

1
I have a library called qtutils which can be used to run an arbitrary method in the main thread. This could be used to call figure updating code from a Python thread, while ensuring it is run in the main thread. Documentation is here. Most people, however, would instead use a QThread and emit a signal which is connected to a method in the main thread. - three_pineapples
Check my answer on this similar question: stackoverflow.com/a/24821300/2319400 - sebastian
These examples help a lot but from what I've seen so far there is no way for me to set the thread up so that it can exit and all of your example seem to contain an set up for the thread to exit. I'll edit my question with more details. - tobook

1 Answers

0
votes

Instead of python threads, use QThread and make your own worker class as suggested in the documentation. (2nd example with the WorkerThread).