回答編集履歴

2

pyscreeze の仕様変更について追記

2019/06/30 12:33

投稿

nunukim
nunukim

スコア135

test CHANGED
@@ -39,3 +39,49 @@
39
39
  pg.alert(text='探索失敗', button='OK')
40
40
 
41
41
  ```
42
+
43
+
44
+
45
+
46
+
47
+ # コメントを受けての追記: pyscreeze の設定変更の仕方
48
+
49
+
50
+
51
+ `pyautogui` の内部で使われている `pyscreeze` が[2度仕様変更しました](https://github.com/asweigart/pyscreeze/blob/81a94e47b75d6cf614c9f6370b783073cb84d69b/pyscreeze/__init__.py#L48)が、`pyautogui` が2度目の仕様変更に追従していないようです。
52
+
53
+
54
+
55
+
56
+
57
+ このため、`pyscreeze` が2度目の仕様変更の前の振る舞いをするように、以下のように設定してやる必要があります。
58
+
59
+
60
+
61
+ ```
62
+
63
+ import pyautogui as pg
64
+
65
+ from pyscreeze import ImageNotFoundException
66
+
67
+
68
+
69
+ # pyscreeze の設定
70
+
71
+ import pyscreeze
72
+
73
+ pyscreeze.USE_IMAGE_NOT_FOUND_EXCEPTION = True
74
+
75
+
76
+
77
+ try:
78
+
79
+ locate = pg.locateCenterOnScreen('search3.png')
80
+
81
+ pg.click(locate)
82
+
83
+ except ImageNotFoundException:
84
+
85
+ pg.alert(text='探索失敗', button='OK')
86
+
87
+ ```

1

例外の補足の仕方について追記

2019/06/30 12:33

投稿

nunukim
nunukim

スコア135

test CHANGED
@@ -9,3 +9,33 @@
9
9
  これをみると、見つからない場合は `ImageNotFoundException` を投げるという仕様になっています。
10
10
 
11
11
  したがって、例外を用いない方法は不可能です。
12
+
13
+
14
+
15
+ # コメントを受けての追記: 例外の補足の仕方
16
+
17
+
18
+
19
+ `ImageNotFoundException` は `pyscreeze` で定義されていますので、次のように import してから使ってください。
20
+
21
+
22
+
23
+
24
+
25
+ ```
26
+
27
+ from pyscreeze import ImageNotFoundException
28
+
29
+
30
+
31
+ try:
32
+
33
+ locate = pg.locateCenterOnScreen('search3.png')
34
+
35
+ pg.click(locate)
36
+
37
+ except ImageNotFoundException:
38
+
39
+ pg.alert(text='探索失敗', button='OK')
40
+
41
+ ```