Python, ищущий, чтобы переместить текст слева направо - PullRequest
1 голос
/ 02 ноября 2019

Этот код перемещает текст слева направо, используя PyQt4. У меня вопрос, есть ли другие способы сделать это?

# -*-coding:utf8 -*-
from PyQt4.QtGui import (QApplication, QGraphicsView, QPainter, QColor, QWidget,
                         QGraphicsScene, QFont, QLabel, QGraphicsSimpleTextItem,
                         QBrush, QLinearGradient, QPixmap, QGraphicsTextItem, QPushButton,
                         QVBoxLayout, QTextDocument)

from PyQt4.QtCore import (QObject, QPointF, QPropertyAnimation,
                          pyqtProperty, Qt, QAbstractAnimation, QRectF)
import sys

data = [u"I want to make text within an ad bar that moves as there is on the TV screen like this"]


class Text(QObject):

    def __init__(self):
        super(Text, self).__init__()

        doc = QTextDocument("%s" % data[0])
        doc.setDefaultStyleSheet(
            "body { color : green; background-color : black }")
        doc.setDefaultFont(QFont("Times", 20, QFont.Bold))

        self.Text_item = QGraphicsTextItem()
        self.Text_item.setDocument(doc)

    def _set_pos(self, pos):

        self.Text_item.setPos(pos)

    pos = pyqtProperty(QPointF, fset=_set_pos)


class Example(QGraphicsView):
    def __init__(self):
        super(Example, self).__init__()

        self.initView()

    def initView(self):

        self.text = Text()

        self.anim = QPropertyAnimation(self.text, b"pos")
        self.anim.setDuration(19000)
        self.anim.setLoopCount(4)
        self.anim.setStartValue(QPointF(-1850.0, 450.0))
        self.anim.setEndValue(QPointF(800.0, 450.0))

        self.scene = QGraphicsScene(self)
        self.scene.setSceneRect(0, 0, 300, 300)
        self.scene.addItem(self.text.Text_item)
        self.setScene(self.scene)

        self.setWindowTitle("Text moveing")
        self.setRenderHint(QPainter.Antialiasing)

        self.anim.start(QAbstractAnimation.DeleteWhenStopped)


if __name__ == '__main__':

    app = QApplication(sys.argv)
    view = Example()
    rect = QApplication.desktop().availableGeometry()
    view.resize(int(rect.width() * 0.75), int(rect.height() * 0.9))
    view.show()
    sys.exit(app.exec_())
...