回答編集履歴
4
ストップ処理を変更
answer
CHANGED
@@ -18,13 +18,14 @@
|
|
18
18
|
if timer and count < 1000:
|
19
19
|
root.after(10, countup)
|
20
20
|
else:
|
21
|
-
|
21
|
+
cancel_timer()
|
22
22
|
|
23
23
|
def cancel_timer():
|
24
24
|
global timer
|
25
25
|
if timer:
|
26
26
|
root.after_cancel(timer)
|
27
27
|
timer = None
|
28
|
+
print("stop", count)
|
28
29
|
|
29
30
|
def key(event):
|
30
31
|
print("pressed", repr(event.char), "count", count)
|
3
キャンセル処理のサンプルコード追加
answer
CHANGED
@@ -1,4 +1,41 @@
|
|
1
1
|
time.sleepを使うとtkinter自体の動作も止まります。
|
2
2
|
root.afterを使って tkinterから指定時間後に関数を呼び出してもらうようにしましょう。
|
3
3
|
|
4
|
-
root.after(ミリ秒数, 呼び出す関数)
|
4
|
+
root.after(ミリ秒数, 呼び出す関数)
|
5
|
+
|
6
|
+
キャンセル処理のサンプルコードを書いてみました。
|
7
|
+
|
8
|
+
```python
|
9
|
+
import tkinter as tk
|
10
|
+
from tkinter import *
|
11
|
+
|
12
|
+
count = 1
|
13
|
+
|
14
|
+
def countup():
|
15
|
+
global timer
|
16
|
+
global count
|
17
|
+
count += 1
|
18
|
+
if timer and count < 1000:
|
19
|
+
root.after(10, countup)
|
20
|
+
else:
|
21
|
+
print("stop", count)
|
22
|
+
|
23
|
+
def cancel_timer():
|
24
|
+
global timer
|
25
|
+
if timer:
|
26
|
+
root.after_cancel(timer)
|
27
|
+
timer = None
|
28
|
+
|
29
|
+
def key(event):
|
30
|
+
print("pressed", repr(event.char), "count", count)
|
31
|
+
cancel_timer()
|
32
|
+
|
33
|
+
root = tk.Tk()
|
34
|
+
frame = Frame(root, width=100, height=100)
|
35
|
+
frame.bind("<Key>", key)
|
36
|
+
frame.focus_set()
|
37
|
+
frame.pack()
|
38
|
+
timer = root.after(10000, cancel_timer)
|
39
|
+
root.after(10, countup)
|
40
|
+
root.mainloop()
|
41
|
+
```
|
2
fix typo
answer
CHANGED
@@ -1,4 +1,4 @@
|
|
1
1
|
time.sleepを使うとtkinter自体の動作も止まります。
|
2
|
-
root.afterを使って
|
2
|
+
root.afterを使って tkinterから指定時間後に関数を呼び出してもらうようにしましょう。
|
3
3
|
|
4
4
|
root.after(ミリ秒数, 呼び出す関数)
|
1
ミリ をカタカナに変更
answer
CHANGED
@@ -1,4 +1,4 @@
|
|
1
1
|
time.sleepを使うとtkinter自体の動作も止まります。
|
2
2
|
root.afterを使って tkinerから指定時間後に関数を呼び出してもらうようにしましょう。
|
3
3
|
|
4
|
-
root.after(
|
4
|
+
root.after(ミリ秒数, 呼び出す関数)
|