回答編集履歴
1
d
test
CHANGED
@@ -17,3 +17,149 @@
|
|
17
17
|
|
18
18
|
|
19
19
|
[QScreen::grabWindow](http://doc.qt.io/qt-5/qscreen.html)
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
## 追記
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
すいませんが、コードは長いので読んでいません。
|
28
|
+
|
29
|
+
QSCreen.grabWindow() の使い方ですが、指定した ID のスクリーンから指定した範囲の画像をキャプチャして返します。
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
> QPixmap grabWindow(WId window, int x = 0, int y = 0, int width = -1, int height = -1)
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
渡している引数が正しければ、そのようなエラーはでないはずなので、引数を確認してみてください。
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
## サンプルコード
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
以下はボタンをクリックすると、スクリーンを grabWindow() で取得して表示するサンプルコードです。
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
```python
|
50
|
+
|
51
|
+
import sys
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
from PyQt5.QtCore import *
|
56
|
+
|
57
|
+
from PyQt5.QtGui import *
|
58
|
+
|
59
|
+
from PyQt5.QtWidgets import *
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
class MainWindow(QMainWindow):
|
66
|
+
|
67
|
+
def __init__(self):
|
68
|
+
|
69
|
+
super().__init__()
|
70
|
+
|
71
|
+
self.initUI()
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
def initUI(self):
|
76
|
+
|
77
|
+
self.setGeometry(300, 300, 500, 500)
|
78
|
+
|
79
|
+
self.setWindowTitle('Test')
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
centralWidget = QWidget(self)
|
84
|
+
|
85
|
+
self.setCentralWidget(centralWidget)
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
# ラベルを作成
|
90
|
+
|
91
|
+
self.label = QLabel()
|
92
|
+
|
93
|
+
self.label.setFixedSize(400, 400)
|
94
|
+
|
95
|
+
# ボタンを作成
|
96
|
+
|
97
|
+
self.button = QPushButton("capture")
|
98
|
+
|
99
|
+
self.button.setFixedSize(400, 50)
|
100
|
+
|
101
|
+
self.button.clicked.connect(self.display)
|
102
|
+
|
103
|
+
# レイアウトを作成
|
104
|
+
|
105
|
+
vbox = QVBoxLayout()
|
106
|
+
|
107
|
+
vbox.addWidget(self.button)
|
108
|
+
|
109
|
+
vbox.addWidget(self.label)
|
110
|
+
|
111
|
+
centralWidget.setLayout(vbox)
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
self.show()
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
def display(self):
|
120
|
+
|
121
|
+
print('capture screen')
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
screen = QApplication.primaryScreen()
|
126
|
+
|
127
|
+
screenRect = QApplication.desktop().screenGeometry()
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
print("window id: ", QApplication.desktop().winId())
|
132
|
+
|
133
|
+
pixmap = screen.grabWindow(
|
134
|
+
|
135
|
+
QApplication.desktop().winId(),
|
136
|
+
|
137
|
+
0, 0, screenRect.width(), screenRect.height())
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
scaled = pixmap.scaled(
|
142
|
+
|
143
|
+
self.label.width(), self.label.height(), Qt.KeepAspectRatio)
|
144
|
+
|
145
|
+
self.label.setPixmap(scaled)
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
if __name__ == '__main__':
|
152
|
+
|
153
|
+
app = QApplication(sys.argv)
|
154
|
+
|
155
|
+
w = MainWindow()
|
156
|
+
|
157
|
+
sys.exit(app.exec_())
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
```
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+

|