回答編集履歴
2
アニメーションを追加
answer
CHANGED
@@ -20,4 +20,45 @@
|
|
20
20
|

|
21
21
|
|
22
22
|
注意点は、自分のウィンドウの左上の座標が (0, 0) になります。
|
23
|
-
ウィンドウ外への移動も 負の値を設定する等で可能。
|
23
|
+
ウィンドウ外への移動も 負の値を設定する等で可能。
|
24
|
+
|
25
|
+
----
|
26
|
+
|
27
|
+
追記: ゆっくりマウスカーソルを移動
|
28
|
+

|
29
|
+
|
30
|
+
```python
|
31
|
+
import tkinter as tk
|
32
|
+
from tkinter import ttk
|
33
|
+
import time
|
34
|
+
|
35
|
+
if __name__ == '__main__':
|
36
|
+
root = tk.Tk()
|
37
|
+
root.geometry("200x200+10+10")
|
38
|
+
|
39
|
+
def mouse_move_to(to_x, to_y, step=100, interval=0.01):
|
40
|
+
def mouse_move_func(event):
|
41
|
+
x = event.x
|
42
|
+
y = event.y
|
43
|
+
step_x = (to_x - event.x) / step
|
44
|
+
step_y = (to_y - event.y) / step
|
45
|
+
|
46
|
+
for _ in range(step):
|
47
|
+
x += step_x
|
48
|
+
y += step_y
|
49
|
+
root.event_generate('<Motion>', warp=True, x=x, y=y)
|
50
|
+
|
51
|
+
# この関数が終わるまでmainloop() は呼ばれません。
|
52
|
+
# update()で明示的にカーソル位置更新。
|
53
|
+
time.sleep(interval)
|
54
|
+
root.update()
|
55
|
+
else:
|
56
|
+
# 最終的な位置調整
|
57
|
+
root.event_generate('<Motion>', warp=True, x=to_x, y=to_y)
|
58
|
+
return mouse_move_func
|
59
|
+
|
60
|
+
button = ttk.Button(root, text="move mouse cursor")
|
61
|
+
button.bind("<1>", mouse_move_to(20, 20))
|
62
|
+
button.pack(fill=tk.BOTH, expand=1)
|
63
|
+
root.mainloop()
|
64
|
+
```
|
1
変数名修正
answer
CHANGED
@@ -12,8 +12,8 @@
|
|
12
12
|
def clicked():
|
13
13
|
# マウスカーソルを移動
|
14
14
|
root.event_generate('<Motion>', warp=True, x=20, y=20)
|
15
|
-
|
15
|
+
button = ttk.Button(root, text="move mouse cursor", command=clicked)
|
16
|
-
|
16
|
+
button.pack(fill=tk.BOTH, expand=1)
|
17
17
|
root.mainloop()
|
18
18
|
```
|
19
19
|
|