Consider the following example:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from mplayer import *
class mplayerStarter(QMainWindow):
def __init__(self, parent=N开发者_C百科one):
super(mplayerStarter, self).__init__(parent)
the_button = QPushButton('Start Mplayer')
the_button.clicked.connect(self.start)
self.setCentralWidget(the_button)
def start(self):
player = Player()
player.loadfile('/tmp/video.mp4')
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
form = mplayerStarter()
form.show()
app.exec_()
If I invoke the program and press the button, the video appears but vanishes immediatly. Any idea how to fix this?
I think it might be being garbage collected. Try this:
class mplayerStarter(QMainWindow):
def __init__(self, parent=None):
super(mplayerStarter, self).__init__(parent)
the_button = QPushButton('Start Mplayer')
the_button.clicked.connect(self.start)
self.setCentralWidget(the_button)
self.player = None
def start(self):
self.player = Player()
self.player.loadfile('/tmp/video.mp4')
精彩评论