在Ubuntu中實(shí)現(xiàn)PyQt多線程可以遵循以下步驟:
from PyQt5.QtCore import QThread, pyqtSignal
class MyThread(QThread):
# 定義一個(gè)信號,用于在多線程中發(fā)送消息
signal = pyqtSignal(str)
def __init__(self, parent=None):
super(MyThread, self).__init__(parent)
def run(self):
for i in range(5):
# 發(fā)送消息到主線程
self.signal.emit('Current value: {}'.format(i))
self.sleep(1)
class MyWindow(QWidget):
def __init__(self):
super(MyWindow, self).__init__()
self.thread = MyThread()
self.thread.signal.connect(self.update_message)
self.thread.start()
layout = QVBoxLayout()
self.label = QLabel()
layout.addWidget(self.label)
self.setLayout(layout)
def update_message(self, message):
self.label.setText(message)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
通過以上步驟,就可以在Ubuntu中實(shí)現(xiàn)PyQt多線程。在多線程中可以執(zhí)行一些耗時(shí)操作,而不會阻塞主線程,從而提高程序的響應(yīng)性。