Skip to content
Advertisement

how to select a particlular portion of a window’s client area to display in window’s thumbnail in the taskbar in Python/PyQt5/PySide2/Tkinter?

I want to set a particular portion(like only a frame or widget) of my application window to taskbar thumbnail.I found one windows API that is ITaskbarList3::SetThumbnailClip here but this is in C++.I want to do this in python.However PyQt5 contains one class that is Qt Windows Extras which doesnot include this function.I have also found something that show one example here.This link might help you to solve easily. As I am a beginner ,i dont know how to do that properly.

Edit And Fix Me:-

I have tried the second link provided in the question,I have installed comptypes Module through PIP and i have copied taskbarlib.tlb file from github as i have not Windos SDK installed to generate this file as said in the answer here.

Here is my code.

from PyQt5.QtWidgets import *
import sys
##########----IMPORTING Comtypes module
import comtypes.client as cc
cc.GetModule('taskbarlib.tlb')
import comtypes.gen.TaskbarLib as tb
taskbar=cc.CreateObject('{56FDF344-FD6D-11d0-958A-006097C9A090}',interface=tb.ITaskbarList3)
##################
class MainUiClass(QMainWindow):
     def __init__(self,parent=None):
        super(MainUiClass,self).__init__(parent)
        self.resize(600,400)
        self.frame=QFrame(self)
        self.frame.setGeometry(100,100,400,200)
        self.frame.setStyleSheet('background:blue')
if __name__=='__main__':
      app=QApplication(sys.argv)
      GUI=MainUiClass()
      GUI.show()
      taskbar.HrInit()
      ####--This is just to check its working or not by setting a tasbar progress bar value
      taskbar.SetProgressValue(GUI.winId(),40,100)
      ##########################################
      ##########This is a method to get the LP_RECT instace as per Microsoft API doc.Using Ctypes and got ctypes.wintypes.RECT object  
      from ctypes import POINTER, WINFUNCTYPE, windll, WinError
      from ctypes.wintypes import BOOL, HWND, RECT
      prototype = WINFUNCTYPE(BOOL, HWND, POINTER(RECT))
      paramflags = (1, "hwnd"), (2, "lprect")
      GetWindowRect = prototype(("GetWindowRect", windll.user32), paramflags)
      newrect=GetWindowRect(int(GUI.frame.winId()))
      print(newrect)
      taskbar.SetThumbnailClip(int(GUI.winId()),newrect)
      sys.exit(app.exec_())

i have got that method to find LP_RECT instance form Ctypes module Doc. hereenter image description here But I have a problem I want to set the frame(blue colored) into taskbar and i got thisenter image description here

Look at the screenshot,the taskbarprogress value that i have set for testing, works fine ,but the thumbnail part is just Unexpected. Can anyone help me to fix this?

Advertisement

Answer

Thanks to Eliya Duskwight to help me slove this. I have corrected my code.

This is For PyQt5 And PySide2 as well as Tkinter

Here is mycode for PyQt5/PySide2

from PyQt5.QtWidgets import *          ###For PyQt5
from PySide2.QtWidgets import *        ###For PySide2
import sys
###Most Important Part###
import comtypes.client as cc
cc.GetModule('taskbarlib.tlb')
import comtypes.gen.TaskbarLib as tb
taskbar=cc.CreateObject('{56FDF344-FD6D-11d0-958A-006097C9A090}',interface=tb.ITaskbarList3)
class MainUiClass(QMainWindow):
   def __init__(self,parent=None):
        super(MainUiClass,self).__init__(parent)
        self.resize(600,400)
        self.setStyleSheet('background:red')
        self.frame=QFrame(self)
        self.frame.setGeometry(100,100,400,200)
        self.frame.setStyleSheet('background:blue')
if __name__=='__main__':
      app=QApplication(sys.argv)
      GUI=MainUiClass()
      GUI.show()
      taskbar.HrInit()
      ##You need to find "hwnd" of your Window and "LP_RECT" instance of Geometry you want, as per Microsoft API Doc. using Ctypes
      from ctypes.wintypes import  RECT,PRECT 
      newrect=PRECT(RECT(0,0,600,400))
      taskbar.SetThumbnailClip(int(GUI.winId()),PRECT(RECT(0,0,600,400)))

Not only you can use this API for thumbnailclip but also for various functions like ThumbnailToolTip,Taskbar Progress,Taskbar Progressbar,Taskbar Overlay Icon,Adding Buttons etc.You need to read the interface here

>>> For Tkinter

from tkinter import *
from ctypes import windll
from ctypes.wintypes import  RECT,PRECT
###Most Important Part###
import comtypes.client as cc
cc.GetModule('taskbarlib.tlb')
import comtypes.gen.TaskbarLib as tb
taskbar=cc.CreateObject('{56FDF344-FD6D-11d0-958A-006097C9A090}',interface=tb.ITaskbarList3)
root=Tk()
root.geometry("400x300")
root.config(bg="red")
frame=Frame(root,bg='blue',width=200,height=100)
frame.place(x=100,y=100)

def setThumbnail():
   hwnd = windll.user32.GetParent(root.winfo_id())
   print(hwnd)
   taskbar.HrInit()
   taskbar.SetThumbnailClip(hwnd,PRECT(RECT(0,0,60,60)))#Geometry You Want To Set
root.after(1000,setThumbnail)   
root.mainloop()

I found root.winfo_id() doesnot give the correct hwnd.I dont know why?So i used hwnd = windll.user32.GetParent(root.winfo_id()) to find the correct one.

Note: This works after the window is visible,So for PyQt5/PySide2,it should be called after calling window.show() & For Tkinter,it should be called after sometime,for that you can use root.after() Or Some event, and You should have that taskbarlib.tlb file pasted in your module folder or in your script folder.You can also generate it using Windows SDK or IDL compiler.Then install comtypes module to work.And remember

The return of S_OK ,means the return of SetThumbnailClip() is 0 always.so anything except 0 is a error.This might crash the application.And As per Microsoft API,the Minimum Required OS is Windows7.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement