如何在 QTextEdit 中自动滚动文本(动画效果)? [英] How to scroll text in QTextEdit automatically (animational effect)?

查看:185
本文介绍了如何在 QTextEdit 中自动滚动文本(动画效果)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问如何在QTextEdit scoll中制作文字,达到动画效果.动画效果应该类似于视频中显示的内容:https://www.youtube.com/watch?v=MyeuGdXv4XM

I would like to ask how to make the text in QTextEdit scoll, to achieve an animational effect. The animational effect should be something like what in the video shows: https://www.youtube.com/watch?v=MyeuGdXv4XM

使用 PyQt 我想得到这样的效果:文本应以 2 行/秒的速度自动向下滚动,直到到达结尾并停止.

With PyQt I want to get this effect: The text should be scolled automatically at a speed of 2 lines/second downwards, till it reaches the end and stops.

在我下面的代码中,当单击按钮时,文本显示在 QTextEdit-Widget 中.文本很长,所以显示滚动条.

In my code below, when the button is clicked, the text is shown in QTextEdit-Widget. The text is very long, so that the scroll bar is shown.

我的问题:我不知道如何制作动画效果.因此,我想请您帮忙更正我的代码.

My Problem: I dont know how to make the animation effect. Thus I would like to ask your help to correct my code.

# -*- coding: utf-8 -*-

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

import sys
import time


list_longText = [" long text 1 - auto scrolling " * 1000, " long text 2 - auto scrolling " * 2000]

class Worker(QObject):
    finished = pyqtSignal()
    strTxt = pyqtSignal(str)

    def __init__(self, parent=None):
        super(Worker, self).__init__(parent)

    @pyqtSlot()
    def onJob(self):
        for i in range(2):
            self.strTxt.emit(list_longText[i])
            time.sleep(2)

class MyApp(QWidget):
    def __init__(self):
        super(MyApp, self).__init__()
        self.setFixedSize(600, 400)
        self.setObjectName("window")

        self.initUI()


    def initUI(self):

        self.txt = QTextEdit("", self)
        self.btn = QPushButton("Button", self)
        self.btn.clicked.connect(self.start)

        self.layout = QHBoxLayout(self)
        self.layout.addWidget(self.txt)
        self.layout.addWidget(self.btn)
        self.setLayout(self.layout)

        self.show()

    def start(self):
        self.thread = QThread()
        self.obj = Worker()

        self.obj.strTxt.connect(self.showText)
        self.obj.moveToThread(self.thread)
        self.obj.finished.connect(self.thread.quit)
        self.thread.started.connect(self.obj.onJob)
        self.thread.start()

    def showText(self, str):
        self.txt.setText("{}".format(str))
        self.autoScroll()

    def autoScroll(self):
        vsb = self.txt.verticalScrollBar()
        if vsb.value() <= vsb.maximum():
            vsb.setValue(vsb.value() + 2)
            time.sleep(1)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyApp()
    sys.exit(app.exec_())

非常感谢您的帮助!

推荐答案

你想要的任务不重,是周期性的,所以使用线程是不合适的,对于这个任务我们可以使用QVariantAnimation.

The task you want is not heavy, it is periodic so using a thread is inappropriate, for this task we can use QVariantAnimation.

另一部分是创建一个移动到某一行文本的方法,我们在QTextDocumentfindBlockByLineNumber()旁边使用QTextCursor代码>.

The other part is to create a method that moves to a certain line of text for it we use QTextCursor next to findBlockByLineNumber() of QTextDocument.

对于最后一个,我们必须开始移动到最后一个可见的初始位置,我们通过 viewport() 的大小使用 cursorForPosition() 方法.

And for the last one we must start moving to the last initial visible for it we use the cursorForPosition() method through the size of the viewport().

longText = "\n".join(["{}: long text - auto scrolling ".format(i) for i in range(100)])

class AnimationTextEdit(QTextEdit):
    def __init__(self, *args, **kwargs):
        QTextEdit.__init__(self, *args, **kwargs)
        self.animation = QVariantAnimation(self)
        self.animation.valueChanged.connect(self.move)

    @pyqtSlot()
    def startAnimation(self):
        self.animation.stop()
        lines_per_second = 2
        self.moveToLine(0)
        p = QPoint(self.viewport().width() - 1, self.viewport().height() - 1)
        cursor = self.cursorForPosition(p)
        self.animation.setStartValue(cursor.blockNumber())
        self.animation.setEndValue(self.document().blockCount()-1)
        self.animation.setDuration(self.animation.endValue()*1000/lines_per_second)
        self.animation.start()

    @pyqtSlot(QVariant)
    def move(self, i):
        cursor = QTextCursor(self.document().findBlockByLineNumber(i))
        self.setTextCursor(cursor)

class MyApp(QWidget):
    def __init__(self):
        super(MyApp, self).__init__()
        self.setFixedSize(600, 400)
        self.txt = AnimationTextEdit(self)
        self.btn = QPushButton("Start", self)
        self.layout = QHBoxLayout(self)
        self.layout.addWidget(self.txt)
        self.layout.addWidget(self.btn)
        self.txt.append(longText)
        self.txt.move(0)
        self.btn.clicked.connect(self.txt.startAnimation)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

更新:

如果你想连续移动你必须使用verticalScrollBar():

if you want a continuous movement you must use verticalScrollBar():

longText = "\n".join(["{}: long text - auto scrolling ".format(i) for i in range(100)])

class AnimationTextEdit(QTextEdit):
    def __init__(self, *args, **kwargs):
        QTextEdit.__init__(self, *args, **kwargs)
        self.animation = QVariantAnimation(self)
        self.animation.valueChanged.connect(self.moveToLine)

    @pyqtSlot()
    def startAnimation(self):
        self.animation.stop()
        self.animation.setStartValue(0)
        self.animation.setEndValue(self.verticalScrollBar().maximum())
        self.animation.setDuration(self.animation.endValue()*4)
        self.animation.start()

    @pyqtSlot(QVariant)
    def moveToLine(self, i):
        self.verticalScrollBar().setValue(i)

class MyApp(QWidget):
    def __init__(self):
        super(MyApp, self).__init__()
        self.setFixedSize(600, 400)
        self.txt = AnimationTextEdit(self)
        self.btn = QPushButton("Start", self)
        self.layout = QHBoxLayout(self)
        self.layout.addWidget(self.txt)
        self.layout.addWidget(self.btn)
        self.txt.append(longText)
        self.txt.moveToLine(0)
        self.btn.clicked.connect(self.txt.startAnimation)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

这篇关于如何在 QTextEdit 中自动滚动文本(动画效果)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆