【发布时间】:2014-09-23 06:30:53
【问题描述】:
我可以使用 PyQt 从桌面或任何窗口截取屏幕截图吗?如何处理桌面上的keyPressEvent?
谢谢。
【问题讨论】:
我可以使用 PyQt 从桌面或任何窗口截取屏幕截图吗?如何处理桌面上的keyPressEvent?
谢谢。
【问题讨论】:
桌面截图示例:
import sys
from PyQt4.QtGui import QPixmap, QApplication
app = QApplication(sys.argv)
QPixmap.grabWindow(QApplication.desktop().winId()).save('screenshot.jpg', 'jpg')
如果您想截取特定窗口的屏幕截图,请将QApplication.desktop() 替换为您要截取屏幕截图的小部件。
【讨论】:
搜索你想要的标题项目
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)
然后用标题截图
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")
【讨论】:
要获取 App-Window 使用:
ex = Ui_MainWindow() #The class wher you call self.show()
QPixmap.grabWidget(ex).save('screenshot.jpg', 'jpg')
【讨论】: