from PySide6.QtWidgets import QApplication, QMainWindow, QTextEdit, QPushButton, QVBoxLayout, QWidget
from PySide6.QtCore import Qt
from PySide6.QtGui import QTextCursor
import sys
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("实时打印功能按钮输出")
# 创建中央小部件和布局
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout(central_widget)
# 创建QTextEdit控件
self.text_edit = QTextEdit()
self.text_edit.setReadOnly(True)
layout.addWidget(self.text_edit)
# 创建功能按钮
self.function_button = QPushButton("执行功能")
self.function_button.clicked.connect(self.on_function_button_clicked)
layout.addWidget(self.function_button)
def on_function_button_clicked(self):
# 执行功能
self.perform_function()
def perform_function(self):
# 假设这是你的代码的一部分,它会实时产生输出
for i in range(5):
# 执行一些任务,这里只是打印文本
output = f"功能输出: {i}\n"
self.update_text_edit(output)
# 暂停一段时间以模拟实时输出
QApplication.processEvents()
import time
time.sleep(0.5)
def update_text_edit(self, text):
# 将文本添加到QTextEdit中
cursor = self.text_edit.textCursor()
cursor.movePosition(QTextCursor.End) # 使用QTextCursor.End来移动光标到文本末尾
cursor.insertText(text)
self.text_edit.setTextCursor(cursor)
self.text_edit.ensureCursorVisible()
# 创建应用和主窗口
app = QApplication([])
window = MainWindow()
window.show()
sys.exit(app.exec())
PySide6设计GUI,在Text上实时打印功能按钮输出
最新推荐文章于 2024-08-09 11:35:30 发布