python 滚动字幕_python – 在PyQt中滚动文本?

我正在尝试让feedparser中的文本从右到左滚动屏幕.我正在使用PyQt5,我不知道如何添加此功能.

我想要显示的是下面

import feedparser

sports = feedparser.parse('http://rssfeeds.usatoday.com/UsatodaycomSports-TopStories')

for e in sports['entries']:

news = (e.get('title', ''))

我正在寻找连续滚动,直到所有新闻标题都被阅读,然后页面被重新加载以获取最新的头条新闻或者只​​是重读已经存在的新闻.谢谢!

解决方法:

您可以使用QTimeLine在标签中显示连续滚动的新闻切片.如果在QTimeLine运行时应用程序中的其他功能被阻止,我会在一个小的gui中实现它来尝试:

import feedparser

import sys

from PyQt5 import QtWidgets, QtCore

class MyWidget(QtWidgets.QWidget):

def __init__(self, parent = None):

QtWidgets.QWidget.__init__(self, parent)

self.setGeometry(200, 200, 800, 600)

self.textLabel = QtWidgets.QLabel('') # label showing some text

self.uButton = QtWidgets.QPushButton('upper Button')

self.lButton = QtWidgets.QPushButton('lower Button')

self.label = QtWidgets.QLabel('') # label showing the news

self.label.setAlignment(QtCore.Qt.AlignRight) # text starts on the right

self.layout = QtWidgets.QVBoxLayout()

self.layout.addWidget(self.textLabel)

self.layout.addWidget(self.uButton)

self.layout.addWidget(self.lButton)

self.layout.addWidget(self.label)

self.layout.setStretch(0, 3)

self.layout.setStretch(1, 3)

self.layout.setStretch(2, 3)

self.layout.setStretch(3, 1)

self.setLayout(self.layout)

self.timeLine = QtCore.QTimeLine()

self.timeLine.setCurveShape(QtCore.QTimeLine.LinearCurve) # linear Timeline

self.timeLine.frameChanged.connect(self.setText)

self.timeLine.finished.connect(self.nextNews)

self.signalMapper = QtCore.QSignalMapper(self)

self.signalMapper.mapped[str].connect(self.setTlText)

self.uButton.clicked.connect(self.signalMapper.map)

self.signalMapper.setMapping(self.uButton, self.uButton.text())

self.lButton.clicked.connect(self.signalMapper.map)

self.signalMapper.setMapping(self.lButton, self.lButton.text())

self.feed()

def feed(self):

fm = self.label.fontMetrics()

self.nl = int(self.label.width()/fm.averageCharWidth()) # shown stringlength

news = []

sports = feedparser.parse('http://rssfeeds.usatoday.com/UsatodaycomSports-TopStories')

for e in sports['entries']:

news.append(e.get('title', ''))

appendix = ' '*self.nl # add some spaces at the end

news.append(appendix)

delimiter = ' +++ ' # shown between the messages

self.news = delimiter.join(news)

newsLength = len(self.news) # number of letters in news = frameRange

lps = 4 # letters per second

dur = newsLength*1000/lps # duration until the whole string is shown in milliseconds

self.timeLine.setDuration(dur)

self.timeLine.setFrameRange(0, newsLength)

self.timeLine.start()

def setText(self, number_of_frame):

if number_of_frame < self.nl:

start = 0

else:

start = number_of_frame - self.nl

text = '{}'.format(self.news[start:number_of_frame])

self.label.setText(text)

def nextNews(self):

self.feed() # start again

def setTlText(self, text):

string = '{} pressed'.format(text)

self.textLabel.setText(string)

if __name__ == '__main__':

app = QtWidgets.QApplication(sys.argv)

widget = MyWidget()

widget.show()

sys.exit(app.exec_())

添加PySide2版本:

import feedparser

import sys

from PySide2 import QtWidgets, QtCore

class MyWidget(QtWidgets.QWidget):

def __init__(self, parent = None):

QtWidgets.QWidget.__init__(self, parent)

self.setGeometry(200, 200, 800, 600)

self.textLabel = QtWidgets.QLabel('') # label showing some text

self.uButton = QtWidgets.QPushButton('upper Button')

self.lButton = QtWidgets.QPushButton('lower Button')

self.label = QtWidgets.QLabel('') # label showing the news

self.label.setAlignment(QtCore.Qt.AlignRight) # text starts on the right

self.layout = QtWidgets.QVBoxLayout()

self.layout.addWidget(self.textLabel)

self.layout.addWidget(self.uButton)

self.layout.addWidget(self.lButton)

self.layout.addWidget(self.label)

self.layout.setStretch(0, 3)

self.layout.setStretch(1, 3)

self.layout.setStretch(2, 3)

self.layout.setStretch(3, 1)

self.setLayout(self.layout)

self.timeLine = QtCore.QTimeLine()

self.timeLine.setCurveShape(QtCore.QTimeLine.LinearCurve) # linear Timeline

self.timeLine.frameChanged.connect(self.setText)

self.timeLine.finished.connect(self.nextNews)

self.signalMapper = QtCore.QSignalMapper(self)

self.signalMapper.mapped[str].connect(self.setTlText)

self.uButton.clicked.connect(self.signalMapper.map)

self.signalMapper.setMapping(self.uButton, self.uButton.text())

self.lButton.clicked.connect(self.signalMapper.map)

self.signalMapper.setMapping(self.lButton, self.lButton.text())

self.feed()

def feed(self):

fm = self.label.fontMetrics()

self.nl = int(self.label.width()/fm.averageCharWidth()) # shown stringlength

news = []

sports = feedparser.parse('http://rssfeeds.usatoday.com/UsatodaycomSports-TopStories')

for e in sports['entries']:

news.append(e.get('title', ''))

appendix = ' '*self.nl # add some spaces at the end

news.append(appendix)

delimiter = ' +++ ' # shown between the messages

self.news = delimiter.join(news)

newsLength = len(self.news) # number of letters in news = frameRange

lps = 4 # letters per second

dur = newsLength*1000/lps # duration until the whole string is shown in milliseconds

self.timeLine.setDuration(dur)

self.timeLine.setFrameRange(0, newsLength)

self.timeLine.start()

def setText(self, number_of_frame):

if number_of_frame < self.nl:

start = 0

else:

start = number_of_frame - self.nl

text = '{}'.format(self.news[start:number_of_frame])

self.label.setText(text)

def nextNews(self):

self.feed() # start again

def setTlText(self, text):

string = '{} pressed'.format(text)

self.textLabel.setText(string)

if __name__ == '__main__':

app = QtWidgets.QApplication(sys.argv)

widget = MyWidget()

widget.show()

sys.exit(app.exec_())

标签:python,label,pyqt

来源: https://codeday.me/bug/20191003/1848065.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值