Question

I am creating a simple media and metdata application using HTML/CSS/Javascript and Python (using PyQt4). I am trying to make a video player using phonon, but I don't have any experience with PyQt4. This is my code:

#!/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_()

I am very lost and confused, if someone could tell me the things I'm doing wrong, it would be much appreciated.

EDIT: Had UiType instead of Ui. Changed that but I still get an error which I don't understand, as video.ui has a button called ppButton.

EDIT 2: Got window to show, but it is empty. It should contain a video player and buttons.

Was it helpful?

Solution

First you have to generate the python code for your video.ui use pyuic4 tool for that.

pyuic4 -x video.ui -o video.py

The above line will generate a module called video.py that will containg a class named as you named UI_

then you can modify you code like this:

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_()

The only issue about this approach is that you have to update you video.py module each time you modify video.ui. But that is a very easy job, just generate it again with:

pyuic4 -x video.ui -o video.py

as you saw before.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top