当我使用PyQt时,我的嵌入式YouTube视频无法显示

时间:2017-06-20 12:06:06

标签: python html youtube-api

我正在使用PyCharm并使用MacPorts导入PyQt4。我从Youtube API页面获得了HTML代码。我从这个网站得到另一半:https://wiki.python.org/moin/PyQt/Embedding%20Widgets%20in%20Web%20Pages

这两个代码组合在一起:

import sys
from PyQt4.QtCore import QSize, Qt
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

html = \
"""<iframe id="existing-iframe-example"
        width="640" height="360"
        src="https://www.youtube.com/embed/DjCO41GMiRg?enablejsapi=1"
        frameborder="0"
        style="border: solid 4px #37474F"
></iframe>

<script type="text/javascript">
  var tag = document.createElement('script');
  tag.id = 'iframe-demo';
  tag.src = 'https://www.youtube.com/iframe_api';
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  var player;
  function onYouTubeIframeAPIReady() {
    player = new YT.Player('existing-iframe-example', {
        events: {
          'onReady': onPlayerReady,
          'onStateChange': onPlayerStateChange
        }
    });
  }
  function onPlayerReady(event) {
    document.getElementById('existing-iframe-example').style.borderColor = '#FF6D00';
  }
  function changeBorderColor(playerStatus) {
    var color;
    if (playerStatus == -1) {
      color = "#37474F"; // unstarted = gray
    } else if (playerStatus == 0) {
      color = "#FFFF00"; // ended = yellow
    } else if (playerStatus == 1) {
      color = "#33691E"; // playing = green
    } else if (playerStatus == 2) {
      color = "#DD2C00"; // paused = red
    } else if (playerStatus == 3) {
      color = "#AA00FF"; // buffering = purple
    } else if (playerStatus == 5) {
      color = "#FF6DOO"; // video cued = orange
    }
    if (color) {
      document.getElementById('existing-iframe-example').style.borderColor = color;
    }
  }
  function onPlayerStateChange(event) {
    changeBorderColor(event.data);
  }
</script>
"""

class WebWidget(QWidget):
    def paintEvent(self, event):
        painter = QPainter()
        painter.begin(self)
        painter.setBrush(Qt.white)
        painter.setPen(Qt.black)
        painter.drawRect(self.rect().adjusted(0, 0, -1, -1))
        painter.setBrush(Qt.red)
        painter.setPen(Qt.NoPen)
        painter.drawRect(self.width() / 4, self.height() / 4,
                         self.width() / 2, self.height() / 2)
        painter.end()

    def sizeHint(self):
        return QSize(100, 100)

class WebPluginFactory(QWebPluginFactory):
    def __init__(self, parent=None):
        QWebPluginFactory.__init__(self, parent)

    def create(mimeType):
        if mimeType == "x-pyqt/widget":
            return WebWidget()

    def plugins(self):
        plugin = QWebPluginFactory.Plugin()
        plugin.name = "PyQt Widget"
        plugin.description = "An example Web plugin written with PyQt."
        mimeType = QWebPluginFactory.MimeType()
        mimeType.name = "x-pyqt/widget"
        mimeType.description = "PyQt widget"
        mimeType.fileExtensions = []
        plugin.mimeTypes = [mimeType]
        print("plugins")
        return [plugin]

if __name__ == "__main__":

    app = QApplication(sys.argv)
    QWebSettings.globalSettings().setAttribute(QWebSettings.PluginsEnabled, True)
    view = QWebView()
    factory = WebPluginFactory()
    view.page().setPluginFactory(factory)
    view.setHtml(html)
    view.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

从PyQt4切换到PySide

我也有这个问题,我想我可能会有答案。 PyQt4不是用Python开发Qt应用程序的唯一选择,还有PySide


您可以通过pip安装它: pip install -U PySide


或程序包管理器(如果使用Linux): sudo apt-get install python-pyside 或对于python3: sudo apt-get install python3-pyside


安装完成后,将PyQt4导入替换为:

from PySide.QtCore import QSize, Qt
from PySide.QtGui import *
from PySide.QtWebKit import *

所有内容和功能应完全相同,希望YouTube能够正常运行。

另一个好处是PySide拥有LGPL许可证,而PyQt4拥有GPL许可证,因此,在使用PySide时,您不必释放程序的源代码。