回答編集履歴

2

アニメーションを追加

2020/04/09 12:25

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -43,3 +43,85 @@
43
43
  注意点は、自分のウィンドウの左上の座標が (0, 0) になります。
44
44
 
45
45
  ウィンドウ外への移動も 負の値を設定する等で可能。
46
+
47
+
48
+
49
+ ----
50
+
51
+
52
+
53
+ 追記: ゆっくりマウスカーソルを移動
54
+
55
+ ![ゆっくりマウスカーソルを移動](4fad40d382fdce37b2ac38088db5eb5c.gif)
56
+
57
+
58
+
59
+ ```python
60
+
61
+ import tkinter as tk
62
+
63
+ from tkinter import ttk
64
+
65
+ import time
66
+
67
+
68
+
69
+ if __name__ == '__main__':
70
+
71
+ root = tk.Tk()
72
+
73
+ root.geometry("200x200+10+10")
74
+
75
+
76
+
77
+ def mouse_move_to(to_x, to_y, step=100, interval=0.01):
78
+
79
+ def mouse_move_func(event):
80
+
81
+ x = event.x
82
+
83
+ y = event.y
84
+
85
+ step_x = (to_x - event.x) / step
86
+
87
+ step_y = (to_y - event.y) / step
88
+
89
+
90
+
91
+ for _ in range(step):
92
+
93
+ x += step_x
94
+
95
+ y += step_y
96
+
97
+ root.event_generate('<Motion>', warp=True, x=x, y=y)
98
+
99
+
100
+
101
+ # この関数が終わるまでmainloop() は呼ばれません。
102
+
103
+ # update()で明示的にカーソル位置更新。
104
+
105
+ time.sleep(interval)
106
+
107
+ root.update()
108
+
109
+ else:
110
+
111
+ # 最終的な位置調整
112
+
113
+ root.event_generate('<Motion>', warp=True, x=to_x, y=to_y)
114
+
115
+ return mouse_move_func
116
+
117
+
118
+
119
+ button = ttk.Button(root, text="move mouse cursor")
120
+
121
+ button.bind("<1>", mouse_move_to(20, 20))
122
+
123
+ button.pack(fill=tk.BOTH, expand=1)
124
+
125
+ root.mainloop()
126
+
127
+ ```

1

変数名修正

2020/04/09 12:25

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -26,9 +26,9 @@
26
26
 
27
27
  root.event_generate('<Motion>', warp=True, x=20, y=20)
28
28
 
29
- label = ttk.Button(root, text="move mouse cursor", command=clicked)
29
+ button = ttk.Button(root, text="move mouse cursor", command=clicked)
30
30
 
31
- label.pack(fill=tk.BOTH, expand=1)
31
+ button.pack(fill=tk.BOTH, expand=1)
32
32
 
33
33
  root.mainloop()
34
34