在 PySide 中创建选框效果 [英] Creating a Marquee effect in PySide

查看:91
本文介绍了在 PySide 中创建选框效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Python 中的 PySide 标签中创建选取框效果?我在列表中有一些新闻标题,我希望它们在窗口底部滚动.

解决方案

您可以创建自定义小部件,但为了避免实现许多方法的任务,请利用 QLabel 类,如下所示:

class MarqueeLabel(QLabel):def __init__(self, parent=None):QLabel.__init__(self, parent)self.px = 0self.py = 15self._direction = Qt.LeftToRightself.setWordWrap(True)self.timer = QTimer(self)self.timer.timeout.connect(self.update)self.timer.start(40)self._speed = 2self.textLength = 0self.fontPointSize = 0self.setAlignment(Qt.AlignVCenter)self.setFixedHeight(self.fontMetrics().height())def setFont(self, font):QLabel.setFont(self, font)self.setFixedHeight(self.fontMetrics().height())def updateCoordinates(self):align = self.alignment()如果对齐 == Qt.AlignTop:self.py = 10elif align == Qt.AlignBottom:self.py = self.height() - 10elif 对齐 == Qt.AlignVCenter:self.py = self.height()/2self.fontPointSize = self.font().pointSize()/2self.textLength = self.fontMetrics().width(self.text())def setAlignment(self,alignment):self.updateCoordinates()QLabel.setAlignment(自我,对齐)def resizeEvent(self, event):self.updateCoordinates()QLabel.resizeEvent(self, event)defpaintEvent(self, event):画家 = QPainter(self)如果 self._direction == Qt.RightToLeft:self.px -= self.speed()如果 self.px <= -self.textLength:self.px = self.width()别的:self.px += self.speed()如果 self.px >= self.width():self.px = -self.textLengthPainter.drawText(self.px, self.py + self.fontPointSize, self.text())画家翻译(self.px,0)定义速度(自我):返回 self._speeddef setSpeed(self, speed):self._speed = 速度def setDirection(自我,方向):self._direction = 方向如果 self._direction == Qt.RightToLeft:self.px = self.width() - self.textLength别的:self.px = 0自我更新()定义暂停(自我):self.timer.stop()def取消暂停(自我):self.timer.start()

可以在以下

How would I be able to create a marquee effect in a PySide Label in Python? I have some news headlines in a list and I would like them to scroll across the bottom of the window.

解决方案

You could create a custom widget but to avoid the task of implementing many methods takes advantage of the QLabel class as shown below:

class MarqueeLabel(QLabel):
    def __init__(self, parent=None):
        QLabel.__init__(self, parent)
        self.px = 0
        self.py = 15
        self._direction = Qt.LeftToRight
        self.setWordWrap(True)
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update)
        self.timer.start(40)
        self._speed = 2
        self.textLength = 0
        self.fontPointSize = 0
        self.setAlignment(Qt.AlignVCenter)
        self.setFixedHeight(self.fontMetrics().height())

    def setFont(self, font):
        QLabel.setFont(self, font)
        self.setFixedHeight(self.fontMetrics().height())

    def updateCoordinates(self):
        align = self.alignment()
        if align == Qt.AlignTop:
            self.py = 10
        elif align == Qt.AlignBottom:
            self.py = self.height() - 10
        elif align == Qt.AlignVCenter:
            self.py = self.height() / 2
        self.fontPointSize = self.font().pointSize() / 2
        self.textLength = self.fontMetrics().width(self.text())

    def setAlignment(self, alignment):
        self.updateCoordinates()
        QLabel.setAlignment(self, alignment)

    def resizeEvent(self, event):
        self.updateCoordinates()
        QLabel.resizeEvent(self, event)

    def paintEvent(self, event):
        painter = QPainter(self)
        if self._direction == Qt.RightToLeft:
            self.px -= self.speed()
            if self.px <= -self.textLength:
                self.px = self.width()
        else:
            self.px += self.speed()
            if self.px >= self.width():
                self.px = -self.textLength
        painter.drawText(self.px, self.py + self.fontPointSize, self.text())
        painter.translate(self.px, 0)

    def speed(self):
        return self._speed

    def setSpeed(self, speed):
        self._speed = speed

    def setDirection(self, direction):
        self._direction = direction
        if self._direction == Qt.RightToLeft:
            self.px = self.width() - self.textLength
        else:
            self.px = 0
        self.update()

    def pause(self):
        self.timer.stop()

    def unpause(self):
        self.timer.start()

The following example can be found in the following link.

class Example(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setWindowTitle("Marquee Effect")
        self.setLayout(QVBoxLayout())
        self.marqueeLabel = MarqueeLabel(self)
        flayout = QFormLayout()
        self.layout().addLayout(flayout)
        le = QLineEdit(self)
        le.textChanged.connect(self.marqueeLabel.setText)
        le.setText("""Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.""")
        slider = QSlider(Qt.Horizontal, self)
        slider.valueChanged.connect(self.marqueeLabel.setSpeed)
        slider.setValue(10)

        rtl = QRadioButton("Right to Left", self)
        ltr = QRadioButton("Left to Rigth", self)
        rtl.toggled.connect(lambda state: self.marqueeLabel.setDirection(Qt.RightToLeft if state else  Qt.LeftToRight))
        ltr.setChecked(True)

        directionWidget = QWidget(self)
        directionWidget.setLayout(QHBoxLayout())
        directionWidget.layout().setContentsMargins(0, 0, 0, 0)
        directionWidget.layout().addWidget(rtl)
        directionWidget.layout().addWidget(ltr)
        fontBtn = QPushButton("Font...", self)
        fontBtn.clicked.connect(self.changeFont)
        colorBtn = QPushButton("Color...", self)
        colorBtn.clicked.connect(self.changeColor)
        pauseBtn = QPushButton("Pause", self)
        pauseBtn.setCheckable(True)
        pauseBtn.toggled.connect(lambda state: self.marqueeLabel.pause() if state else self.marqueeLabel.unpause())
        pauseBtn.toggled.connect(lambda state: pauseBtn.setText("Resume") if state else pauseBtn.setText("Pause"))

        flayout.addRow("Change Text", le)
        flayout.addRow("Change Speed", slider)
        flayout.addRow("Direction", directionWidget)
        flayout.addRow("fontBtn", fontBtn)
        flayout.addRow("colorBtn", colorBtn)
        flayout.addRow("Animation", pauseBtn)
        self.layout().addWidget(self.marqueeLabel)

    def changeColor(self):
        palette = self.marqueeLabel.palette()
        color = QColorDialog.getColor(palette.brush(QPalette.WindowText).color(), self)
        if color.isValid():
            palette.setBrush(QPalette.WindowText, color)
            self.marqueeLabel.setPalette(palette)

    def changeFont(self):
        font, ok = QFontDialog.getFont(self.marqueeLabel.font(), self)
        if ok:
            self.marqueeLabel.setFont(font)


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    w = Example()
    w.show()
    sys.exit(app.exec_())

Output:

这篇关于在 PySide 中创建选框效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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