PyQt6 ------ 如何在 QTextEditor 组件中按顺序递增显示提示信息

PyQt6 ------ 如何在 QTextEditor 组件中按顺序递增显示提示信息

  • 推荐阅读
  • 正文

推荐阅读

PyQt6 ------ 如何在 QLineEditor 组件中按顺序显示提示信息

正文

在推荐阅读中我们已经介绍过背景了,这里直接上代码:

from PyQt6.QtCore import Qt, pyqtSlot, QTimer
from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QTextEdit, QVBoxLayout, QWidget
from gui_file import YourCalculator


class YourGUI(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setFixedSize(800, 600)
        self.calculator = YourCalculator()

        self.calculator.task_completed.connect(self.on_task_completed)

        button = QPushButton("Start Tasks")
        button.clicked.connect(self.start_tasks)
        button.setFixedSize(200, 20)

        self.text_editor = QTextEdit("hhh")
        self.text_editor.setFixedSize(200, 200)

        widget = QWidget(self)
        widget.setFixedSize(400, 600)

        layout = QVBoxLayout(widget)
        layout.addWidget(button)
        layout.addWidget(self.text_editor)

        self.timer = None
        self.task_index = None

    def start_tasks(self):
        self.task_index = 1
        self.timer = QTimer()
        self.timer.timeout.connect(self.execute_tasks)
        self.timer.start(500)

    def execute_tasks(self):
        if self.task_index <= 3:
            task_method = getattr(self.calculator, f"task{self.task_index}")
            task_method()
            self.task_index += 1

        else:
            self.timer.stop()

    @pyqtSlot(str)
    def on_task_completed(self, message):
        current_text = self.text_editor.toPlainText()
        new_text = current_text + '\n' + message
        self.text_editor.setText(new_text)


if __name__ == "__main__":
    app = QApplication([])
    window = YourGUI()
    window.show()
    app.exec()

如果大家觉得有用,就请点个赞吧~

你可能感兴趣的:(PyQt5,python,PyQt5)