02-09-2018, 02:04 PM
(Última modificación: 02-09-2018, 02:27 PM por konniskatt.)
Hola, tengo un código que crea un reproductor de video.
Lo que pasa es que quiero crear otro botón (ya hay un boton, que es el de play), pero quiero ubicarlo abajo de ese botón, ya que ahora lo esta colocando al lado del boton de play.
El botón ya está creado, es fullSCR
También tengo otros problemas, pj: la slidebar para controlar, si el usuario quiere cambiar la posición, hay que agarrar con el mouse el cuadrado de la slidebar, sin embargo no debería ser así, el usuario simplemente debería dar click en el lugar donde se quiera mover la slidebar (espero que se entienda) y también el QVideoWidget no se extiende completamente por la pantalla, queda unas pequeñas barras, (que son del fondo de la ventana)
Saludos
Lo que pasa es que quiero crear otro botón (ya hay un boton, que es el de play), pero quiero ubicarlo abajo de ese botón, ya que ahora lo esta colocando al lado del boton de play.
El botón ya está creado, es fullSCR
Código:
[code]
class VideoWindow(QMainWindow):
def __init__(self, parent=None):
super(VideoWindow, self).__init__(parent)
# Customizing window
self.setWindowTitle('Video')
self.setWindowIcon(QIcon('vid_icon.svg'))
setwinicon()
# Starting media player
self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
videoWidget = QVideoWidget()
self.playButton = QPushButton()
self.playButton.setEnabled(False)
self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
self.playButton.clicked.connect(self.play)
self.fullSCR = QPushButton()
self.fullSCR.setEnabled(True)
#self.fullSCR.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
self.fullSCR.clicked.connect(self.t_fullscreen)
self.positionSlider = QSlider(Qt.Horizontal)
self.positionSlider.setRange(0, 0)
self.positionSlider.sliderMoved.connect(self.setPosition)
self.errorLabel = QLabel()
self.errorLabel.setSizePolicy(QSizePolicy.Preferred,
QSizePolicy.Maximum)
# Create new action
openAction = QAction(QIcon('icons/16x16/actions/document-open.png'), \n '&Open', self)
openAction.setShortcut('Ctrl+O')
openAction.setStatusTip('Open movie')
openAction.triggered.connect(self.openFile)
# Create exit action
exitAction = QAction(QIcon('icons/16x16/actions/system-log-out.png'), \n '&Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(self.exitCall)
changeresAction = QAction(QIcon('icons/16x16/apps/help-browser.png'), \n '&Change resolution', self)
# aboutAction.setShortcut('')
aboutAction = QAction(QIcon('icons/16x16/apps/help-browser.png'), \n '&About...', self)
aboutAction.setShortcut('Ctrl+I')
aboutAction.setStatusTip('About video')
aboutAction.triggered.connect(self.about)
fullscreenAction = QAction(QIcon('icons/16x16/devices/video_icon.png'), \n '&Enter fullscreen', self)
fullscreenAction.setShortcut('F11')
fullscreenAction.setStatusTip('Toggle fullscreen mode')
fullscreenAction.triggered.connect(self.t_fullscreen)
self.playAction = QAction(QIcon('icons/16x16/actions/media-playback-start.png'), \n '&Play', self)
# playAction.setShortcut('')
self.playAction.triggered.connect(self.playnp)
self.playAction.setEnabled(False)
self.pauseAction = QAction(QIcon('icons/16x16/actions/media-playback-pause.png'), \n '&Pause', self)
# playAction.setShortcut('')
self.pauseAction.triggered.connect(self.pausenp)
self.pauseAction.setEnabled(False)
# Create menu bar and add action
self.menuBar = self.menuBar()
fileMenu = self.menuBar.addMenu('&File')
# fileMenu.addAction(newAction)
fileMenu.addAction(openAction)
fileMenu.addAction(exitAction)
playbackMenu = self.menuBar.addMenu('&Playback')
playbackMenu.addAction(self.playAction)
playbackMenu.addAction(self.pauseAction)
toolsMenu = self.menuBar.addMenu('&Tools')
toolsMenu.addAction(fullscreenAction)
self.chgresm = toolsMenu.addMenu('Change resolution')
self.chgresm.setEnabled(False)
chgres43 = self.chgresm.addAction('4:3')
chgres169 = self.chgresm.addAction('16:9')
# changeresAction.setStatusTip('Change resolution')
chgres43.triggered.connect(self.screen43)
chgres169.triggered.connect(self.screen169)
helpMenu = self.menuBar.addMenu('&Help')
helpMenu.addAction(aboutAction)
# Create a widget for window contents
wid = QWidget(self)
self.setCentralWidget(wid)
# Create layouts to place inside widget
controlLayout = QHBoxLayout()
controlLayout.setContentsMargins(0, 0, 0, 0)
controlLayout.addWidget(self.playButton)
controlLayout.addWidget(self.fullSCR)
controlLayout.addWidget(self.positionSlider)
layout = QVBoxLayout()
layout.addWidget(videoWidget)
layout.addLayout(controlLayout)
layout.addWidget(self.errorLabel)
# Set widget to contain window contents
wid.setLayout(layout)
self.mediaPlayer.setVideoOutput(videoWidget)
self.mediaPlayer.stateChanged.connect(self.mediaStateChanged)
self.mediaPlayer.positionChanged.connect(self.positionChanged)
self.mediaPlayer.durationChanged.connect(self.durationChanged)
self.mediaPlayer.error.connect(self.handleError)
def t_fullscreen(self):
if self.windowState() & Qt.WindowFullScreen:
self.showNormal()
#self.menuBar.setVisible(True)
loginfo(infotype="i", cstr="No fullscreen")
else:
self.showFullScreen()
#self.menuBar.setVisible(False)
loginfo(infotype="i", cstr="Fullscreen entered")
def exitCall(self):
loginfo(infotype="i", cstr="Good bye!")
sys.exit(app.exec_())
def play(self):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
self.mediaPlayer.pause()
self.pauseAction.setEnabled(False)
else:
self.mediaPlayer.play()
self.pauseAction.setEnabled(True)
def playnp(self):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
pass
else:
self.mediaPlayer.play()
self.pauseAction.setEnabled(True)
self.playAction.setEnabled(False)
def pausenp(self):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
self.mediaPlayer.pause()
self.playAction.setEnabled(True)
self.pauseAction.setEnabled(False)
else:
pass
def openFile(self):
fileName, _ = QFileDialog.getOpenFileName(self, "Open Movie",
QDir.homePath(), """Videos (*.mp4 *.avi *.wmv *.mpeg *.mpg *.mkv *.webm *.m4v *.ogg *.ts)
Music (*.mp3 *.mp2 *.ogg *.flac *.wav *.m4a)
Images (*.png *.jpeg *.jpg *.gif *.bmp)
Subtitles (*.sub)
All files (*)""")
# fileName = "C:/Users/nicol/Desktop/Mikel Erentxun - A Un Minuto De Ti (video clip).mp4"
if fileName != '':
self.mediaPlayer.setMedia(
QMediaContent(QUrl.fromLocalFile(fileName)))
self.playButton.setEnabled(True)
self.playAction.setEnabled(False)
self.chgresm.setEnabled(True)
fileName, fileExt = os.path.splitext(fileName)
if fileExt == '.sub':
pass
loginfo(infotype="i", cstr="File opened: %s" % fileName)
self.setWindowTitle("Video - %s" % fileName)
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
self.mediaPlayer.pause()
self.pauseAction.setEnabled(False)
else:
self.mediaPlayer.play()
self.pauseAction.setEnabled(True)
def keyPressEvent(self, e):
if e.key() == Qt.Key_Space:
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
self.mediaPlayer.pause()
self.playAction.setEnabled(True)
self.pauseAction.setEnabled(False)
else:
self.mediaPlayer.play()
self.pauseAction.setEnabled(True)
self.playAction.setEnabled(False)
if e.key() == Qt.Key_F11:
self.t_fullscreen()
def about(self):
msglo = (
'''
By konniskatt
''')
QMessageBox.about(self, "About video app", msglo)
def mediaStateChanged(self, state):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
self.playButton.setIcon(
self.style().standardIcon(QStyle.SP_MediaPause))
else:
self.playButton.setIcon(
self.style().standardIcon(QStyle.SP_MediaPlay))
def positionChanged(self, position):
self.positionSlider.setValue(position)
def durationChanged(self, duration):
self.positionSlider.setRange(0, duration)
def setPosition(self, position):
self.mediaPlayer.setPosition(position)
def handleError(self):
self.playButton.setEnabled(False)
loginfo(infotype="e", cstr="^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
def screen169(self):
self.widescreen = True
mwidth = self.frameGeometry().width()
mheight = self.frameGeometry().height()
mleft = self.frameGeometry().left()
mtop = self.frameGeometry().top()
mratio = 1.778
self.setGeometry(mleft, mtop, mwidth, mwidth / mratio)
def screen43(self):
self.widescreen = False
mwidth = self.frameGeometry().width()
mheight = self.frameGeometry().height()
mleft = self.frameGeometry().left()
mtop = self.frameGeometry().top()
mratio = 1.33
self.setGeometry(mleft, mtop, mwidth, mwidth / mratio)
[/code]
Saludos