I have a while loop which is set to True , inside the loop I want to write some text to TextBrowser but no matter what I try , self.textEdit.setText or self.textEdit.append doesn't work. I tried to change also label or even line text but without success. when I take this out of the while loop the text appear in the text browser. I treid using Threads but even when the thread is started the text does not appear in the text browser. here is the code i have.
import socket
import socket as socket_library
import threading
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1014, 920)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.radioButton = QtWidgets.QRadioButton(self.centralwidget)
self.radioButton.setGeometry(QtCore.QRect(180, 180, 141, 41))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.radioButton.setFont(font)
self.radioButton.setObjectName("radioButton")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(180, 230, 691, 411))
self.textEdit.setStyleSheet("background-color: rgb(0, 0, 0);\n"
"color: rgb(0, 255, 0);")
self.textEdit.setObjectName("textEdit")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(210, 80, 571, 51))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.radioButton.setText(_translate("MainWindow", "Start Server"))
self.label.setText(_translate("MainWindow",
"<html><head/><body><p><span style=\" color:#00557f;\">Lots of Trips Server</span></p></body></html>"))
self.radioButton.clicked.connect(self.check_readio_button)
def check_readio_button(self):
if self.radioButton.isChecked():
self.activate_server()
def activate_server(self):
hostname = socket.gethostname()
IPaddress = socket.gethostbyname(hostname)
port = 45971
server_socket = socket_library.socket(socket_library.AF_INET, socket_library.SOCK_DGRAM)
server_socket.bind((IPaddress, port))
while True:
try:
message, address = server_socket.recvfrom(1024)
except ConnectionError:
print("Connection with server is closed.")
break
server_socket.sendto("Thank you for connection.".encode("UTF-8"), address)
answer = message.decode("UTF-8")
self.thread1 = threading.Thread(target=self.task(), args=(None,))
self.thread1.start()
self.thread1.join()
server_socket.close()
def task(self):
self.textEdit.setText("This is from another thread")
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_())
Thread.start()
Thread.join()
CodePudding user response:
I think "append" is working, but you need to process pending events and refresh the GUI by calling QApplication.processEvents()