簡體   English   中英

如何 select 窗口客戶區的特定部分顯示在 Python/PyQt5/PySide2/Tkinter 任務欄中的窗口縮略圖中?

[英]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. 由於我是初學者,我不知道如何正確地做到這一點。

編輯修復我:-

我已經嘗試了問題中提供的第二個鏈接,我已經通過PIP安裝了comptypes模塊,並且我已經從github復制了 taskbarlib.tlb文件,因為我沒有安裝 Windows ZF20E3C5E54C0AB3D3765D660B3F8 的答案來生成文件。

這是我的代碼。

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_())

我有這種方法可以從Ctypes 模塊文檔中找到 LP_RECT 實例。 這里在此處輸入圖像描述 但我有一個問題,我想將框架(藍色)設置到任務欄中,我得到了這個在此處輸入圖像描述

查看屏幕截圖,我為測試設置的任務欄進度值工作正常,但縮略圖部分只是Unexpected 誰能幫我解決這個問題?

感謝Eliya Duskwight幫助我解決這個問題。 我已經更正了我的代碼。

這適用於PyQt5 和 PySide2以及Tkinter

這是 PyQt5/PySide2 的 mycode

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)))

您不僅可以將此API用於縮略圖剪輯,還可以用於縮略圖工具提示、任務欄進度、任務欄進度條、任務欄覆蓋圖標、添加按鈕等各種功能。您需要在此處閱讀界面

>>>適用於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()

我發現root.winfo_id()沒有給出正確的hwnd 。我不知道為什么?所以我用hwnd = windll.user32.GetParent(root.winfo_id())來找到正確的。

注意:這在 window 可見后有效,因此對於PyQt5/PySide2 ,應在調用window.show()后調用它 & 對於Tkinter或某些時候可以使用root。事件,並且您應該將該taskbarlib.tlb 文件粘貼到您的模塊文件夾或腳本文件夾中。您也可以使用Windows SDK 或 IDL 編譯器生成它。然后安裝comtypes模塊以工作。記住

S_OK 的返回,意味着 SetThumbnailClip() 的返回始終為 0。因此,除 0 之外的任何內容都是錯誤。這可能會使應用程序崩潰。並且根據 Microsoft API,最低要求的操作系統是 Windows7。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM