質問するログイン新規登録

回答編集履歴

2

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

2019/06/30 12:33

投稿

nunukim
nunukim

スコア135

answer CHANGED
@@ -18,4 +18,27 @@
18
18
  pg.click(locate)
19
19
  except ImageNotFoundException:
20
20
  pg.alert(text='探索失敗', button='OK')
21
+ ```
22
+
23
+
24
+ # コメントを受けての追記: pyscreeze の設定変更の仕方
25
+
26
+ `pyautogui` の内部で使われている `pyscreeze` が[2度仕様変更しました](https://github.com/asweigart/pyscreeze/blob/81a94e47b75d6cf614c9f6370b783073cb84d69b/pyscreeze/__init__.py#L48)が、`pyautogui` が2度目の仕様変更に追従していないようです。
27
+
28
+
29
+ このため、`pyscreeze` が2度目の仕様変更の前の振る舞いをするように、以下のように設定してやる必要があります。
30
+
31
+ ```
32
+ import pyautogui as pg
33
+ from pyscreeze import ImageNotFoundException
34
+
35
+ # pyscreeze の設定
36
+ import pyscreeze
37
+ pyscreeze.USE_IMAGE_NOT_FOUND_EXCEPTION = True
38
+
39
+ try:
40
+ locate = pg.locateCenterOnScreen('search3.png')
41
+ pg.click(locate)
42
+ except ImageNotFoundException:
43
+ pg.alert(text='探索失敗', button='OK')
21
44
  ```

1

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

2019/06/30 12:33

投稿

nunukim
nunukim

スコア135

answer CHANGED
@@ -3,4 +3,19 @@
3
3
  https://pyautogui.readthedocs.io/en/latest/screenshot.html#the-locate-functions
4
4
 
5
5
  これをみると、見つからない場合は `ImageNotFoundException` を投げるという仕様になっています。
6
- したがって、例外を用いない方法は不可能です。
6
+ したがって、例外を用いない方法は不可能です。
7
+
8
+ # コメントを受けての追記: 例外の補足の仕方
9
+
10
+ `ImageNotFoundException` は `pyscreeze` で定義されていますので、次のように import してから使ってください。
11
+
12
+
13
+ ```
14
+ from pyscreeze import ImageNotFoundException
15
+
16
+ try:
17
+ locate = pg.locateCenterOnScreen('search3.png')
18
+ pg.click(locate)
19
+ except ImageNotFoundException:
20
+ pg.alert(text='探索失敗', button='OK')
21
+ ```