回答編集履歴
1
d
answer
CHANGED
@@ -7,4 +7,77 @@
|
|
7
7
|
|
8
8
|
> QPixmap QScreen::grabWindow(WId window, int x = 0, int y = 0, int width = -1, int height = -1)
|
9
9
|
|
10
|
-
[QScreen::grabWindow](http://doc.qt.io/qt-5/qscreen.html)
|
10
|
+
[QScreen::grabWindow](http://doc.qt.io/qt-5/qscreen.html)
|
11
|
+
|
12
|
+
## 追記
|
13
|
+
|
14
|
+
すいませんが、コードは長いので読んでいません。
|
15
|
+
QSCreen.grabWindow() の使い方ですが、指定した ID のスクリーンから指定した範囲の画像をキャプチャして返します。
|
16
|
+
|
17
|
+
> QPixmap grabWindow(WId window, int x = 0, int y = 0, int width = -1, int height = -1)
|
18
|
+
|
19
|
+
渡している引数が正しければ、そのようなエラーはでないはずなので、引数を確認してみてください。
|
20
|
+
|
21
|
+
## サンプルコード
|
22
|
+
|
23
|
+
以下はボタンをクリックすると、スクリーンを grabWindow() で取得して表示するサンプルコードです。
|
24
|
+
|
25
|
+
```python
|
26
|
+
import sys
|
27
|
+
|
28
|
+
from PyQt5.QtCore import *
|
29
|
+
from PyQt5.QtGui import *
|
30
|
+
from PyQt5.QtWidgets import *
|
31
|
+
|
32
|
+
|
33
|
+
class MainWindow(QMainWindow):
|
34
|
+
def __init__(self):
|
35
|
+
super().__init__()
|
36
|
+
self.initUI()
|
37
|
+
|
38
|
+
def initUI(self):
|
39
|
+
self.setGeometry(300, 300, 500, 500)
|
40
|
+
self.setWindowTitle('Test')
|
41
|
+
|
42
|
+
centralWidget = QWidget(self)
|
43
|
+
self.setCentralWidget(centralWidget)
|
44
|
+
|
45
|
+
# ラベルを作成
|
46
|
+
self.label = QLabel()
|
47
|
+
self.label.setFixedSize(400, 400)
|
48
|
+
# ボタンを作成
|
49
|
+
self.button = QPushButton("capture")
|
50
|
+
self.button.setFixedSize(400, 50)
|
51
|
+
self.button.clicked.connect(self.display)
|
52
|
+
# レイアウトを作成
|
53
|
+
vbox = QVBoxLayout()
|
54
|
+
vbox.addWidget(self.button)
|
55
|
+
vbox.addWidget(self.label)
|
56
|
+
centralWidget.setLayout(vbox)
|
57
|
+
|
58
|
+
self.show()
|
59
|
+
|
60
|
+
def display(self):
|
61
|
+
print('capture screen')
|
62
|
+
|
63
|
+
screen = QApplication.primaryScreen()
|
64
|
+
screenRect = QApplication.desktop().screenGeometry()
|
65
|
+
|
66
|
+
print("window id: ", QApplication.desktop().winId())
|
67
|
+
pixmap = screen.grabWindow(
|
68
|
+
QApplication.desktop().winId(),
|
69
|
+
0, 0, screenRect.width(), screenRect.height())
|
70
|
+
|
71
|
+
scaled = pixmap.scaled(
|
72
|
+
self.label.width(), self.label.height(), Qt.KeepAspectRatio)
|
73
|
+
self.label.setPixmap(scaled)
|
74
|
+
|
75
|
+
|
76
|
+
if __name__ == '__main__':
|
77
|
+
app = QApplication(sys.argv)
|
78
|
+
w = MainWindow()
|
79
|
+
sys.exit(app.exec_())
|
80
|
+
|
81
|
+
```
|
82
|
+
|
83
|
+

|