I
I
Ilya2020-11-16 13:01:17
Python
Ilya, 2020-11-16 13:01:17

How to create an extra thread in PyQt5?

I have 2 scripts:
ui.py - which contains the ui of my program (compiled with ui.ui )
and the main script - program.py - which contains the program logic, there is the main code and the ui change.
There is a function in the program.py script - send_message() which sent a message to the mail using smtp
I call send_message when the button is clicked:
ui.start_button.clicked.connect(send_message)

And everything worked fine, the messages were sent but the ui (interface) was hanging. I googled and realized that I need to do it all from more than one thread, so I created a function that worked when the button was pressed.

import threading
def new_thread():
    thread = threading(target=send_message)
    thread.start()


And again, even the interface worked fine, but there was an error in the console that did not end the program.
The error itself: QObject::setParent: Cannot set parent, new parent is in a different thread
And when the send_message() method ended, the program gave an error, I'll attach it below.
5fb24de2f36e8914802263.png
How can I find a way out of this situation, I would be glad to hear from you

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bbkmzzzz, 2020-11-16
@bbkmzzzz

Use Qt threads. QThread or QThreadPool along with QRunnable

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question