0

I am trying to develop a simple video player using python-vlc and pyqt5. But the problem is that I am unable to make it go fullscreen. When I click the fullscreen button self.frame.showFullScreen() does nothing. I even tried the solution posted here.

But unfortunately it did not help me. I am kinda new to PyQt5 and VLC thing but I am unable to sort it out.

Here is my code:

from PyQt5 import QtCore, QtGui, QtWidgets
import vlc
import time
import os
import platform

class Ui_MainWindow(QtWidgets.QWidget):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        font = QtGui.QFont()
        font.setPointSize(15)
        MainWindow.setFont(font)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.frame = QtWidgets.QFrame(self.centralwidget)
        self.frame.setGeometry(QtCore.QRect(0, 0, 801, 421))
        self.frame.setStyleSheet("background-color: rgb(28, 29, 32);")
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.label = QtWidgets.QLabel(self.frame)
        self.label.setGeometry(QtCore.QRect(0, 0, 791, 421))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.label.setFont(font)
        self.label.setStyleSheet("QLabel{\n"
   "    color: rgb(255, 255, 255)\n"
   "}")
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(70, 469, 241, 91))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(490, 469, 241, 91))
        self.pushButton_2.setObjectName("pushButton_2")
        MainWindow.setCentralWidget(self.centralwidget)

        self.instance = vlc.Instance()
        self.mediaplayer = vlc.MediaPlayer()
        self.pushButton.clicked.connect(self.Load_Video)
        self.pushButton_2.clicked.connect(self.go_fullscreen)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "Click Load Video to play  your favourite videos !"))

        self.pushButton.setText(_translate("MainWindow", "Load Video"))
        self.pushButton_2.setText(_translate("MainWindow", "Fullscreen"))


    def Load_Video(self):
        """Open a media file in a MediaPlayer
        """

        dialog_txt = "Choose Media File"
        filename = QtWidgets.QFileDialog.getOpenFileName(self, dialog_txt, os.path.expanduser('~'))
        if not filename:
            return

        # getOpenFileName returns a tuple, so use only the actual file name
        self.media = self.instance.media_new(filename[0])

        # Put the media in the media player
        self.mediaplayer.set_media(self.media)

        # Set the title of the track as window title
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", self.media.get_meta(0)))

        if platform.system() == "Linux": # for Linux using the X Server
            self.mediaplayer.set_xwindow(int(self.frame.winId()))
        elif platform.system() == "Windows": # for Windows
            self.mediaplayer.set_hwnd(int(self.frame.winId()))
        elif platform.system() == "Darwin": # for MacOS
            self.mediaplayer.set_nsobject(int(self.frame.winId()))

        self.mediaplayer.play()
        time.sleep(2)

    def go_fullscreen(self):
        self.frame.showFullScreen()  # This is the part where the code doesn't work, it does nothing.

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Is there anthing I am doing wrong? if it is so then please let me know.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

0

I don't know (almost) anything about PyQT5 but if I'm referring to the documentation, calling showFullScreen() only affects windows, so you have to detach your frame from the main window before.

def go_fullscreen(self):
    self.frame.setParent(None)
    self.frame.showFullScreen()
Corralien
  • 109,409
  • 8
  • 28
  • 52
  • Thank you very much for your immediate response. It worked. I just had a last question that how can we bring it back again in its original state? Once again Thank you for your response. – Just_A_Coder Apr 24 '21 at 08:36
  • To return from full-screen mode, call `showNormal()`. I think you need *reparent* frame. Probably you need to `installEventFilter()` to catch `Esc` key press or anything else. – Corralien Apr 24 '21 at 13:03
  • Maybe this [link](https://stackoverflow.com/a/59401485/15239951) can help you. The author of this answer is the one who edited your post! – Corralien Apr 24 '21 at 13:05
  • @Just_A_Coder `self.frame.setParent(self.centralWidget); self.show()`; do note that you didn't use any [layout manager](https://doc.qt.io/qt-5/layout.html) (which is usually not a very good idea), so you'll need to reset the geometry manually (and this is one of the reasons for which not using layouts is a really bad choice). Also, avoid trying to edit the output of pyuic files, that's another bad practice. – musicamante Apr 24 '21 at 13:05
  • It Worked. Thank you everyone who helped me to solve this problem. Thank you for you advice musicamante. I will look forward to use Layouts in pyqt5, and I will try not to edit from output of pyuic files. Once again, Thank you everyone who supported me. – Just_A_Coder Apr 24 '21 at 14:51