how to use QWinThumbnailToolBar to restore Taskbar Thumbnail after closing/opening vlc player in PyQt5

407 views Asked by At

I am embedding python vlc in pyqt5 to make a video player.but i am having one problem that is when video starts to play it takes entire taskbar thumbnail to show only its video not other items present in the window screen.And after closing the video the taskbar becomes totally blank white, not showing anything.Here is picture samples. 1-Before Playing enter image description here

2.When Playing(Look there is no button) enter image description here
3.After Stoppedenter image description here

I am trying to restore by

QWinThumbnailToolBar in self.videoframe.showEvent()

Here is my Complete Code:

import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtWinExtras 
import vlc

class Player(QMainWindow):
    """A simple Media Player using VLC and Qt
    """
    def __init__(self, master=None):
        QMainWindow.__init__(self, master)
        self.setWindowTitle("Media Player")
        self.mainframe=QFrame(self)
        self.setCentralWidget(self.mainframe)
        self.videoframe=QFrame(self.mainframe)
        self.videoframe.setGeometry(0,50,600,400)
        ##Call to set taskbar thumbnail
        self.videoframe.showEvent=self.setthumbnail
        self.instance = vlc.Instance()
        self.player = self.instance.media_player_new()
        self.player.set_hwnd(int(self.videoframe.winId()))
        self.media = self.instance.media_new('C:/Users/mishra/Downloads/Video/kyakar.mp4')
        self.player.set_media(self.media)
        button=QPushButton('Play',self)
        button.setStyleSheet('background:red')
        button.setGeometry(100,0,40,30)
        button.clicked.connect(lambda:[self.videoframe.show(),self.player.play()])
        button1=QPushButton('Close',self)
        button1.setStyleSheet('background:green')
        button1.setGeometry(200,0,40,30)
        button1.clicked.connect(self.onclose)
    def onclose(self):
        if self.player:
            self.player.stop()
        self.videoframe.hide()
    def setthumbnail(self,event):
        print('Shown')
        self.thumbbar =QtWinExtras.QWinThumbnailToolBar(self)
        self.thumbbar.setWindow(self.windowHandle())        
    
if __name__ == "__main__":
    app = QApplication(sys.argv)
    player = Player()
    player.show()
    player.resize(600, 450)
    sys.exit(app.exec_())

Is there any way to achieve that?

1

There are 1 answers

1
S. Nick On

I marked for you the lines I made the changes. Try it:

import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtWinExtras 
from PyQt5.QtWinExtras import QWinThumbnailToolBar, QWinThumbnailToolButton
import vlc   
                                         

class Player(QMainWindow):
    """A simple Media Player using VLC and Qt
    """
    def __init__(self, master=None):
        QMainWindow.__init__(self, master)
        
        self.setWindowTitle("Media Player")
        self.mainframe = QFrame(self)
        self.setCentralWidget(self.mainframe)
        
        self.videoframe = QFrame(self.mainframe)
#-        self.videoframe.setGeometry(0, 50, 600, 400)                               # ---
        self.videoframe.resize(640, 480)                                             # +++
        
# +++  vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv +++     
        # QWinThumbnailToolBar
        self.toolBar = QWinThumbnailToolBar(self)        

        # Prev, Play/Pause, Next
        self.toolBtnPrev = QWinThumbnailToolButton(self.toolBar)
        self.toolBtnPrev.setToolTip('Prev')
        self.toolBtnPrev.setIcon(self.style().standardIcon(QStyle.SP_MediaSkipBackward))
        self.toolBtnPrev.clicked.connect(self.set_prev)
        self.toolBar.addButton(self.toolBtnPrev)

        self.toolBtnControl = QWinThumbnailToolButton(self.toolBar)
        self.toolBtnControl.setToolTip('Play')                       
        self.toolBtnControl.setProperty('status', 0)
        self.toolBtnControl.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
        self.toolBtnControl.clicked.connect(self.set_control)
        self.toolBar.addButton(self.toolBtnControl)

        self.toolBtnNext = QWinThumbnailToolButton(self.toolBar)
        self.toolBtnNext.setToolTip('Next')
        self.toolBtnNext.setIcon(self.style().standardIcon(QStyle.SP_MediaSkipForward))
        self.toolBtnNext.clicked.connect(self.set_next)
        self.toolBar.addButton(self.toolBtnNext)
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +++        
        
        
        # Call to set taskbar thumbnail
#-        self.videoframe.showEvent = self.setthumbnail                           # ---
        self.instance = vlc.Instance()
        self.player = self.instance.media_player_new()
        self.player.set_hwnd(int(self.videoframe.winId()))
        self.media = self.instance.media_new('Samonastrojka.avi')                 # !!! <---
        self.player.set_media(self.media)
        '''
        button = QPushButton('Play',self)
        button.setStyleSheet('background:red')
        button.setGeometry(100,0,40,30)
        button.clicked.connect(lambda:[self.videoframe.show(),self.player.play()])
        button1 = QPushButton('Close',self)
        button1.setStyleSheet('background:green')
        button1.setGeometry(200,0,40,30)
        button1.clicked.connect(self.onclose)
        
    def onclose(self):
        if self.player:
            self.player.stop()
        self.videoframe.hide()
        
    def setthumbnail(self,event):
        print('Shown')
        self.thumbbar = QtWinExtras.QWinThumbnailToolBar(self)
        self.thumbbar.setWindow(self.windowHandle())        
        '''
        
# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    def set_prev(self):
        pass

    def set_next(self):
        pass

    def set_control(self):
        if self.toolBtnControl.property('status') == 0:
            self.toolBtnControl.setToolTip('Pause')
            self.videoframe.show()
            self.player.play()
            self.toolBtnControl.setProperty('status', 1)
            self.toolBtnControl.setIcon(self.style().standardIcon(QStyle.SP_MediaPause))
        else:
            self.toolBtnControl.setProperty('status', 0)
            self.toolBtnControl.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))        
            self.player.stop()
            self.toolBtnControl.setToolTip('Play')

    def showEvent(self, event):
        super(Player, self).showEvent(event)
        if not self.toolBar.window():
            self.toolBar.setWindow(self.windowHandle())
        
    
if __name__ == "__main__":
    app = QApplication(sys.argv)
    player = Player()
    player.show()
    player.resize(640, 480)
    sys.exit(app.exec_())

enter image description here