miversen33
miversen33

Reputation: 569

PyQt4 bug when running multiple classes

Ok so maybe it isnt a bug, but I cant get it to work. say you have to classes that both use PyQt4. One is called Audio.py and uses Phonon to play a sound file. the other is called GUI.py and uses QtGui to display a screen. GUI needs to be able to call and use Audio.py whenever it wants (import Audio). It will import and send the call to my Audio class but because Audio is not started (double click to run) it does not the code (app=QApplication(sys.argv); sys.exit(app.exec_())). so while the Audio class runs when it is run, when you import it, it will not play sounds because its own QApplication loop has not been started.

Any Help?

Edit: Added class Engine these are 2 seperate python files (.py)

import Library,Player,sys

from PyQt4.QtGui import QApplication

class Engine(object):

def __init__(self,path,song=None):
    self.counter=0
    self.path=path
    self.lib=Library.Library(self.path)
    if song is None:
        self.player=Player.Player(self.lib.getSong(self.counter))
    else:
        self.player=Player.Player(path+song)

def updatePlayer(self,songStatus):
    self.player.findStatus(songStatus)

def getCurrentSong(self):
    return self.lib.getSong(self.counter)

if __name__=='__main__':

app=QApplication(sys.argv)
e=Engine('D:/Music/','Yeah!.mp3')
e.updatePlayer('Play')
sys.exit(app.exec_())

import sys

from PyQt4.QtGui import QApplication

from PyQt4.QtCore import QObject

from PyQt4.phonon import Phonon

class Player(QObject):

def __init__(self,song):
    super(QObject,self).__init__()
    self.song=song
    self.media=None

    #self.metaInfo=Phonon.MediaObject(self)
    #self.metaInfo.currentSourceChanged.connect(self.disMetaData)

    self.initMedia()
    self.findStatus()


def initMedia(self):
    if not self.media:
        self.media=Phonon.MediaObject()
        audioOutput=Phonon.AudioOutput(Phonon.MusicCategory,self)
        Phonon.createPath(self.media,audioOutput)
    self.media.setCurrentSource(Phonon.MediaSource(self.song))

def findStatus(self,status=None):
    if status is not None:
        if status=='Play':
            self.playSong()
            return

        if status=='Stop':
            self.stopSong()
            return

        if status=='Pause':
            self.pauseSong()
            return

        if status=='Next':
            nextSong()
            return

        if status=='Previous':
            self.previousSong()
            return

def playSong(self):
    self.media.play()

def stopSong(self):
    self.media.stop()

def pauseSong(self):
    self.media.pause()

def nextSong(self):
    '''nextSong code'''

def previousSong(self):
    '''previousSong code'''

if __name__=='__main__':

app=QApplication(sys.argv)
p=Player('D:/Music/Yeah!.mp3')
p.findStatus('Play')
sys.exit(app.exec_())

Upvotes: 1

Views: 325

Answers (1)

warvariuc
warvariuc

Reputation: 59674

To be sure you make actions when the event loop is already running use QTimer::singleShot:

QtCore.QTimer.singleShot(0, some_function)

Upvotes: 1

Related Questions