S
S
sparr0w12021-09-01 09:53:16
PyQt
sparr0w1, 2021-09-01 09:53:16

How to change the text of a Label in PyQt5 from another thread (QThread)?

I am writing a server part for a PC remote control program. I decided to make an interface design in PyQt5,
brought the server launch to a separate thread. And I need to derive a variable from the server class with IP and PORT for connection to show it in the interface! How can I do that? (Design made in Qt Desinger)

import socket
import webbrowser

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QThread

class Control(QThread):
    def __init__(self):
        super(Control, self).__init__()
        self.value = 0

    sig = QtCore.pyqtSignal()

    def run(self):
        HOST = socket.gethostbyname_ex(socket.gethostname())[-1][-1]
        PORT = 6677

        #print(HOST)
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.bind((HOST, PORT))
            s.listen()
            while True:
                conn, addr = s.accept()
                while True:
                    data = conn.recv(1024).decode('utf-8').lower()
                    print(data)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.setFixedSize(850, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.background_color = QtWidgets.QLabel(self.centralwidget)
        self.background_color.setGeometry(QtCore.QRect(0, 0, 850, 600))
        self.background_color.setStyleSheet("background-color: rgb(0, 0, 0);")
        self.background_color.setFrameShadow(QtWidgets.QFrame.Plain)
        self.background_color.setText("")
        self.background_color.setObjectName("background_color")
        self.namelabel = QtWidgets.QLabel(self.centralwidget)
        self.namelabel.setGeometry(QtCore.QRect(0, 0, 850, 600))
        font = QtGui.QFont()
        font.setFamily("SWEETREVENGE")
        font.setPointSize(40)
        font.setBold(True)
        font.setWeight(75)
        self.namelabel.setFont(font)
        self.namelabel.setStyleSheet("QLabel#namelabel{\n"
"    margin-top: 50px;\n"
"    color: rgb(255, 255, 255);\n"
"}")
        self.namelabel.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignTop)
        self.namelabel.setObjectName("namelabel")
        self.ip_port = QtWidgets.QLabel(self.centralwidget)
        self.ip_port.setGeometry(QtCore.QRect(0, 0, 850, 600))
        font = QtGui.QFont()
        font.setFamily("Ubuntu")
        font.setPointSize(20)
        self.ip_port.setFont(font)
        self.ip_port.setStyleSheet("color:white;")
        self.ip_port.setAlignment(QtCore.Qt.AlignCenter)
        self.ip_port.setIndent(0)
        self.ip_port.setOpenExternalLinks(False)
        self.ip_port.setObjectName("ip_port")
        self.result_connect = QtWidgets.QLabel(self.centralwidget)
        self.result_connect.setGeometry(QtCore.QRect(0, 0, 850, 600))
        font = QtGui.QFont()
        font.setFamily("Ubuntu")
        font.setPointSize(19)
        self.result_connect.setFont(font)
        self.result_connect.setStyleSheet("margin-bottom: 100px;\n"
"color: white;")
        self.result_connect.setAlignment(QtCore.Qt.AlignBottom|QtCore.Qt.AlignHCenter)
        self.result_connect.setObjectName("result_connect")
        MainWindow.setCentralWidget(self.centralwidget)
        self.control = Control()
        self.control.start()
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "PC Control"))
        self.namelabel.setText(_translate("MainWindow", "PC CONTROLS"))
        self.ip_port.setText(_translate("MainWindow", "127.0.0.1:6677"))
        self.result_connect.setText(_translate("MainWindow", "Connection Waiting..."))




if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-09-01
@sparr0w1

sig = QtCore.pyqtSignal(str, int)
self.sig.emit(HOST, PORT)
self.control.sig.connect(self.on_signal)

def on_signal(self, host, port):
    self.some_label.setText(host)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question