我使用HTML / CSS / Javascript和Python(使用PyQt4)创建a simple media and metdata application。我正在尝试使用声子制作视频播放器,但我对PyQt4没有任何经验。这是我的代码:
#!/usr/bin/env python
import sys
from PyQt4 import QtCore, QtGui, uic, phonon
class videoPlayer(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
ui = uic.loadUi("video.ui")
media = phonon.Phonon.MediaObject()
playQuery = False
ui..ppButton.clicked.connect(self.playPause)
def playVideo():
media.play()
def pauseVideo():
media.pause()
def playPause():
if playQuery:
pauseVideo()
else:
playVideo()
def changeVideoSource(target):
media.setCurrentSource(phonon.Phonon.MediaSource(target))
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
videoPlayer = videoPlayer()
videoPlayer.show()
app.exec_()
我非常迷茫和困惑,如果有人能告诉我我做错的事情,我将非常感激。
编辑:有UiType
而不是Ui
。改变了但我仍然收到一个我不理解的错误,因为video.ui
有一个名为ppButton
的按钮。
编辑2:有窗口显示,但它是空的。它应该包含一个视频播放器和按钮。
答案 0 :(得分:1)
首先,您必须为video.ui
使用pyuic4
工具生成python代码。
pyuic4 -x video.ui -o video.py
上面一行将生成一个名为video.py
的模块,该模块将包含一个名为UI _的<_p / p>的类
然后您可以像这样修改代码:
import sys
from PyQt4 import QtCore, QtGui, uic, phonon
from video import UI_MyVideoWindow # Lets call it MyVideoWindow for the example.
class videoPlayer(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.ui = UI_MyVideoWindow()
self.ui.setUp(self) # Now you can access you widgets from self.ui
# Example: self.ui.ppButton
media = phonon.Phonon.MediaObject(self) # this object needs a parent.
playQuery = False
def playVideo():
media.play()
def pauseVideo():
media.pause()
def on_ppButton_clicked():
"""
When you generate code using QtDesigner, connections are made automagically ;)
Just write your slots following the below format:
on_<widget_name>_<signal_name>
"""
if playQuery:
pauseVideo()
else:
playVideo()
def changeVideoSource(target):
media.setCurrentSource(phonon.Phonon.MediaSource(target))
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
videoPlayer = videoPlayer()
videoPlayer.show()
app.exec_()
这种方法的唯一问题是每次修改video.py
时都必须更新video.ui
模块。但这是一项非常简单的工作,只需再次生成:
正如你之前看到的那样。pyuic4 -x video.ui -o video.py