Thread on Python
Publicado por Humberto (2 intervenciones) el 01/12/2020 16:59:51
Hello friends, I need help, I am learning to work with visual python, at the moment I am using pyQT, but my question lies in the use of threads.
My application receives data from a device all the time through the Modbus protocol, and this is done in a different thread than the main program of the visual application, that is, from that thread I cannot modify any variable of the main program.
From the thread, Rx_Thread, I need to receive a data and show it in a textEdit of the main program.
My application receives data from a device all the time through the Modbus protocol, and this is done in a different thread than the main program of the visual application, that is, from that thread I cannot modify any variable of the main program.
From the thread, Rx_Thread, I need to receive a data and show it in a textEdit of the main program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from PyQt5 import QtWidgets
from Test_ui import Ui_MainWindow
import sys
from pyModbusTCP.client import ModbusClient
import time, threading
c=ModbusClient(host="127.0.0.1", port=502, auto_open=True)
text = "Hola"
class Main (QtWidgets.QMainWindow):
def __init__(self):
super (Main, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi (self)
def Rx_Thread (): #este es el hilo que recibe los datos
while (True):
reg = c.read_holding_registers(0, 5)
time.sleep(1)
if reg:
#self.ui.textEdit.setPlainText("%s\n" % text) #esto es lo que quiero modificar
print(reg)
else:
print("not")
_thread = threading.Thread(name='Rx_Thread',target=Rx_Thread, daemon = True)
_thread.start()
if __name__ == '__main__':
app=QtWidgets.QApplication([])
main_app = Main()
main_app.show()
sys.exit(app.exec())
Valora esta pregunta


0