我怎样才能在Python中的PySide
Label中创建选取框效果?我在列表中列出了一些新闻标题,我希望他们在窗口底部滚动。在PySide中创建Marquee效果
0
A
回答
1
你可以创建一个自定义窗口小部件,但为了避免在实施许多方法利用了QLabel
类的任务,如下图所示:
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()
下面的例子可以在以下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_())
输出:
+0
如何在不使用QLineEdit函数的情况下设置文本? – Sebiancoder
+0
我想你不想使用我的例子,我创建的小部件也有'setText()'方法,例如,如果你不想让'QLineEdit'注释下面这行:'le.textChanged.connect (self.marqueeLabel.setText)',然后放置'self.marqueeLabel.setText(“your_text”)' – eyllanesc
相关问题
- 1. 在单元格中创建Overstrike效果
- 2. 如何在spritekit中创建风效果
- 3. 在UIScrollView中创建3D效果ios 5
- 4. 如何在AS3中创建Swirl效果?
- 5. 在iOS7中创建模糊效果
- 6. 在锚标记中创建CSS效果
- 7. 在python中创建视觉效果?
- 8. 在Imagemagick中创建投影效果
- 9. 在XNA中创建转换效果
- 10. 如何在JS中创建RollOver效果?
- 11. 如何在PowerShell中创建printf效果
- 12. 在Android中创建摇摆头效果
- 13. 在jQuery中创建幻灯片效果
- 14. 如何在opengl中创建Glass效果?
- 15. 在Windows 8中创建类似于3D效果的效果8
- 16. PySide:创建复选框
- 17. 创建IOS 3D效果
- 18. 创建硬翻页效果
- 19. 创建缩放效果
- 20. 创建画中画(PIP)效果iOS
- 21. tvOS:在UICollectionViewCell上创建视差效果
- 22. 在悬停时创建反弹效果
- 23. 在视图上创建遮罩效果
- 24. 如何制作3D效果类似于Android网站的Marquee?
- 25. 如何创建“图像建设”效果
- 26. 如何动画文字显示在c#中创建打字式效果效果#
- 27. 在Highcharts中,如何在solidgauge中创建辉光/阴影效果?
- 28. 的Python/Pyside:创建图像直方图
- 29. PySide:创建一个自定义QCursor
- 30. PySide/PyQt:如何创建自定义类?
难道我在我的解决方案的工作?如果你这样做,请标记我的答案是正确的。 – eyllanesc
我仍在努力实施它。我忙于上学,所以我没有太多时间来研究它。应该尽快完成。 – Sebiancoder
请花点时间,因为我帮助你,OS中的常见事情是,如果一个正确的答案是给它一个upvote或标记为正确的。询问并且不发表评论,如果他服务阻止社区帮助他。 :P – eyllanesc