Beispiel #1
0
    class MarqueeText(QScrollArea):
        def __init__(self, parent=None):
            QScrollArea.__init__(self)
            self.setParent(parent)
            self.text = ""

            # Set the background colour of the marquee text to white
            self.setStyleSheet("QScrollArea { background-color: rgba(255, 255, 255, 1)}")

            # Initialise the base label and text
            self.label = QLabel()

            # Set the font for marquee
            font = QFont()
            font.setItalic(True)
            font.setBold(True)
            font.setPixelSize(25)
            self.label.setFont(font)

            # Set the base label as the base widget of QScrollArea
            self.setWidget(self.label)

            # Set QScrollBar Policies
            self.setWidgetResizable(True)
            self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

            # Initialise timer and associated variables, used for marquee effect
            self.timer = QTimer(self)
            self.x = 0
            self.speed = 0
            
            # Connect a function to the timer, which controls the marquee effect
            self.timer.timeout.connect(self.updatePos)

            # TODO: Set a nominal speed
            self.setSpeed(33)

        def updatePos(self):
            self.x = (self.x + 1) % self.label.fontMetrics().horizontalAdvance(self.text)
            self.horizontalScrollBar().setValue(self.x)

        def setText(self, text):
            # Sets the text of the marquee label
            # text: (str) the text to be displayed

            # TODO: Change the separator bit when adding universal settings
            self.text = text + "          "
            self.x = 0

            # First need the widths of the current label windowspace and the text itself
            windowWidth = self.window().geometry().width()
            textWidth = self.label.fontMetrics().horizontalAdvance(text)
            # Concatenate the text on itself as many times as needed.
            self.label.setText(self.text + (self.text * (windowWidth // textWidth + (windowWidth % textWidth > 0))))

            # Finally, start the timer to start the effect
            self.timer.start(self.timer.interval())

        def setSpeed(self, speed):
            # Sets the speed of the scroll
            # speed: (int) how many pixels to move per second

            # Reset the timer with new interval.
            self.timer.setInterval(1000 / speed)