from PyQt5 import QtCore, QtWidgets
from PyQt5.QtGui import QColor
Form.setObjectName("Form")
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.verticalLayout.setObjectName("verticalLayout")
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.textBrowser = QtWidgets.QTextBrowser(Form)
self.textBrowser.setObjectName("textBrowser")
self.verticalLayout_2.addWidget(self.textBrowser)
self.verticalLayout.addLayout(self.verticalLayout_2)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem1)
self.verticalLayout.addLayout(self.horizontalLayout)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Example"))
self.pushButton.setText(_translate("Form", "Input"))
class BrowserHandler(QtCore.QObject):
newTextAndColor = QtCore.pyqtSignal(str, object)
self.newTextAndColor.emit(
'{} - thread 2 variant 1.\n'.format(str(time.strftime("%Y-%m-%d-%H.%M.%S", time.localtime()))),
QtCore.QThread.msleep(1000)
self.newTextAndColor.emit(
'{} - thread 2 variant 2.\n'.format(str(time.strftime("%Y-%m-%d-%H.%M.%S", time.localtime()))),
QtCore.QThread.msleep(1000)
class MyWindow(QtWidgets.QWidget):
def __init__(self, parent=None):
self.ui.pushButton.clicked.connect(self.addAnotherTextAndColor)
self.thread = QtCore.QThread()
self.browserHandler = BrowserHandler()
self.browserHandler.moveToThread(self.thread)
self.browserHandler.newTextAndColor.connect(self.addNewTextAndColor)
self.thread.started.connect(self.browserHandler.run)
@QtCore.pyqtSlot(str, object)
def addNewTextAndColor(self, string, color):
self.ui.textBrowser.setTextColor(color)
self.ui.textBrowser.append(string)
def addAnotherTextAndColor(self):
self.ui.textBrowser.setTextColor(QColor(0, 255, 0))
self.ui.textBrowser.append('{} - thread 2 variant 3.\n'.format(str(time.strftime("%Y-%m-%d-%H.%M.%S", time.localtime()))))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)