使用python 截图后 保存图片发现并没有图片

使用python 截图后 保存图片发现并没有图片

代码如下:

import win32gui

from PyQt5.QtWidgets import QApplication
import sys
 
hwnd_title = dict()
 
  
def get_all_hwnd(hwnd, mouse):
    if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):
        hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)})

 
win32gui.EnumWindows(get_all_hwnd, 0)
# print(hwnd_title.items())
for h, t in hwnd_title.items():
    if t != "":
        print(h, t)
 
# 程序会打印窗口的hwnd和title,有了title就可以进行截图了。
hwnd = win32gui.FindWindow(None,"*IDLE Shell 3.11.1*")
app = QApplication(sys.argv)
screen = QApplication.primaryScreen()
#img = screen.grabWindow(QApplication.desktop().winId())
img = screen.grabWindow(hwnd).toImage()

img.save("123.jpg",'jpg')


尝试把路径换成绝对路径,如C:/123.jpg再试试

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^