简体   繁体   English

如何使用 PyQt 抓取桌面截图?

[英]How to grab a desktop screenshot with PyQt?

Can I take a screenshot from desktop or any window with PyQt?我可以使用 PyQt 从桌面或任何窗口截取屏幕截图吗? How to handle keyPressEvent on desktop?如何在桌面上处理 keyPressEvent?

Thanks.谢谢。

Example of how to take a screenshot of the desktop: 如何为桌面截图的示例:

import sys
from PyQt4.QtGui import QPixmap, QApplication

app = QApplication(sys.argv)
QPixmap.grabWindow(QApplication.desktop().winId()).save('screenshot.jpg', 'jpg')

If you want to take a screenshot of a specific window, replace QApplication.desktop() with the widget you want to take a screenshot of. 如果要截取特定窗口的屏幕快照,请将QApplication.desktop()替换为要截屏的窗口小部件。

search the title items which you want搜索您想要的标题项目

import win32gui
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)
 
for h,t in hwnd_title.items():
  if t is not "":
    print(h, t)

then use the title to screenshot然后用标题截图

  from PyQt5.QtWidgets import QApplication
  from PyQt5.QtGui import *
  import win32gui
  import sys

  hwnd = win32gui.FindWindow(None, 'C:\Windows\system32\cmd.exe')
  app = QApplication(sys.argv)
  screen = QApplication.primaryScreen()
  img = screen.grabWindow(hwnd).toImage()
  img.save("screenshot.jpg")

To get the App-Window use: 要获取应用程序窗口,请使用:

ex = Ui_MainWindow() #The class wher you call self.show()
QPixmap.grabWidget(ex).save('screenshot.jpg', 'jpg')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM