1

tengo un problema: cuando trato de usar QGridLayout, la etiqueta (label) no se centra y necesito que haga eso porque necesito hacer que se adapte al cambiar el tamaño de una ventana, y otro problema, cuando trato de importar un video en esa etiqueta, no funciona. ¿Me pueden ayudar por favor? Abajo tengo el código que tengo. Error que tengo

Gracias por su respuesta.

#Internal module(s)
import os
import sys
import subprocess
from colorama import Fore, Back, Style, init
from time import sleep
from PyQt5 import QtWidgets, QtMultimediaWidgets, QtGui, QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QWidget, QMenu, QAction, QBoxLayout, QFrame, QFileDialog, QGridLayout
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtGui import *
from PyQt5.QtCore import *

#Modules
print("Starting the application...")
print("Importing modules...")
from numpy import cbrt
import webbrowser
init()
#Variables
#No internal variables!
#Window class and definitions
def count_3secs_GRJT(timerInSeconds):
    while timerInSeconds:
        minutes, seconds = divmod(timerInSeconds, 60)
        timerInThreeSecondsFmt = "{:2d}:{:02d}".format(minutes, seconds)
        print("Exiting in:", timerInThreeSecondsFmt, end="\r")
        sleep(1)
        timerInSeconds -= 1
    print(Back.BLUE + Fore.LIGHTWHITE_EX + "Program has been ended automatically by pressing the button and waiting some seconds." + Style.RESET_ALL)
    sys.exit(0)
class initializeAppGUIWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        print("Ready.")
        #GUI window
        self.resize(640, 480)
        self.setMinimumWidth(640)
        self.setMinimumHeight(360)
        self.setMaximumWidth(1280)
        self.setMaximumHeight(720)
        self.setWindowTitle("A video playback window.")
        gridLayoutApplication = QGridLayout(self)
        self.setLayout(self.gridLayoutApplication)
        self.videoPlaybackLabelApplication = QLabel("No video has been opened!")
#       self.videoPlaybackLabelApplication.setAlignment(Qt.AlignCenter)
        self.gridLayoutApplication.addWidget(self.videoPlaybackLabelApplication, 0, 0)
#       frameApplication.setStyleSheet("background-color
        videoItemWidgetApp = QVideoWidget()
        openVideoApplication = QAction("&Open video", self)
        openVideoApplication.setStatusTip("Open a video from your PC.\nAccepted formats are MP4, OGG and WEBM.")
        openVideoApplication.triggered.connect(self.openAVideoDialog)
        exitApplication = QAction("&Exit", self)
        exitApplication.triggered.connect(self.mainButtonFunctionalityQuit)
        menuApplicationBar = self.menuBar()
        fileMenuApplicationBar = menuApplicationBar.addMenu("&File")
        fileMenuApplicationBar.addAction(openVideoApplication)
        fileMenuApplicationBar.addAction(exitApplication)
    def mainButtonFunctionalityQuit(self):
        print(Back. BLUE + Fore.LIGHTWHITE_EX + "Program has been ended by pressing the button." + Style.RESET_ALL)
        sys.exit(0)
    def openAVideoDialog(self):
        importVideoDialog = QFileDialog.getOpenFileName(self, "Open a video", "", "MP4 Videos (*.mp4);;OGG Videos (*.ogg);;WEBM Videos (*.webm)")
        if importVideoDialog != "":
            self.videoItemWidgetApp.setMedia(QMediaContent(QUrl.fromLocalFile(importVideoDialog)))
def mainButtonFunctionality1():
    print(Back. BLUE + Fore.LIGHTWHITE_EX + "An URL has been opened by pressing the button." + Style.RESET_ALL)
    webbrowser.open_new_tab("https://github.com/TheGitMpeg/Math-Calc-In-The-Console---0.28.6-BETA/tree/main/0.28.6.1%20Enhanced%20Beta")
def mainButtonFunctionality2():
    print(Back. BLUE + Fore.LIGHTWHITE_EX + "An URL has been opened and the program has been ended by pressing the button." + Style.RESET_ALL)
    webbrowser.open_new_tab("https://github.com/TheGitMpeg/Math-Calc-In-The-Console---0.28.6-BETA/tree/main/0.28.6.1%20Enhanced%20Beta")
    sys.exit(0)
def mainButtonFunctionality3():
    count_3secs_GRJT(3)
    sys.exit(0)
def mainButtonFunctionality4():
    count_3secs_GRJT(5)
    sys.exit(0)
def main():
    generalApplicationGUI = QApplication([])
    applicationLoad = initializeAppGUIWindow()
    applicationLoad.show()
    sys.exit(generalApplicationGUI.exec_())
try:
    main()
except IndexError:
    print(Back.RED + Fore.LIGHTWHITE_EX + "Error! Something went wrong during or before running the application and/or doing an operation! Exiting..." + Style.RESET_ALL)
    exit()

Lenguaje de programación usado: Python Módulo usado para la aplicación: PyQT

Errores/dudas:

  1. ¿Cómo centrar la etiqueta (label) en una celda?
  2. Poner un video en una etiqueta (label)

1 respuesta 1

0

prueba esto:

import sys
from PyQt5 import QtWidgets, QtGui, QtCore,\
    QtMultimedia 
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.Qt import *

    
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.resize(640, 480)
        self.setWindowTitle("A video playback window.")
        
        self.centralWidget = QWidget()                                # +++
        self.setCentralWidget(self.centralWidget)                     # +++
        
        self.gridLayoutApplication = QGridLayout(self.centralWidget)

# +++
        self.videoPlayer = QtMultimedia.QMediaPlayer(self)            # +++
        self.videoItemWidgetApp = QVideoWidget()
        self.videoPlayer.setVideoOutput(self.videoItemWidgetApp)
        self.gridLayoutApplication.addWidget(
            self.videoItemWidgetApp, 0, 0)                
        
        openVideoApplication = QAction("&Open video", self)
        openVideoApplication.setStatusTip(
            "Open a video from your PC.\nAccepted formats are MP4, OGG and WEBM.")
        openVideoApplication.triggered.connect(self.openAVideoDialog)
        exitApplication = QAction("&Exit", self)
        exitApplication.triggered.connect(self.close)
        menuApplicationBar = self.menuBar()
        fileMenuApplicationBar = menuApplicationBar.addMenu("&File")
        fileMenuApplicationBar.addAction(openVideoApplication)
        fileMenuApplicationBar.addAction(exitApplication)
        
    def openAVideoDialog(self):
# ---------------------> vvv    
        importVideoDialog, _ = QFileDialog.getOpenFileName(
            self, 
            "Open a video", 
            "", 
            "MP4 Videos (*.mp4);;OGG Videos (*.ogg);;WEBM Videos (*.webm);;AVI Videos (*.avi)"
        )
        
        if importVideoDialog:                
            print(f'{importVideoDialog}')
            print(f'{_}')
            self.videoPlayer.setMedia(
                QtMultimedia.QMediaContent(QtCore.QUrl(importVideoDialog))
            )
            self.videoPlayer.play()

 
if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

introducir la descripción de la imagen aquí

introducir la descripción de la imagen aquí

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.