如何用PyQt截取桌面截图?
我可以用PyQt从桌面或任何窗口截屏吗?怎么在桌面上处理按键事件呢?
谢谢。
3 个回答
0
要获取应用窗口,可以使用:
ex = Ui_MainWindow() #The class wher you call self.show()
QPixmap.grabWidget(ex).save('screenshot.jpg', 'jpg')
1
搜索你想要的标题项目
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")
8
下面是如何截取桌面屏幕的示例:
import sys
from PyQt4.QtGui import QPixmap, QApplication
app = QApplication(sys.argv)
QPixmap.grabWindow(QApplication.desktop().winId()).save('screenshot.jpg', 'jpg')
如果你想截取某个特定窗口的屏幕,可以把 QApplication.desktop()
替换成你想截取的那个窗口的名字。