回答編集履歴

4

関数名間違えていたのを修正 s/count_down/count_up/

2021/03/12 06:37

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -36,7 +36,7 @@
36
36
 
37
37
 
38
38
 
39
- gentimer.tk(root, count_down(10), root.quit)
39
+ gentimer.tk(root, count_up(10), root.quit)
40
40
 
41
41
  root.mainloop()
42
42
 
@@ -68,7 +68,7 @@
68
68
 
69
69
 
70
70
 
71
- # gen_timer(root, count_down(10))
71
+ # gen_timer(root, count_up(10))
72
72
 
73
73
  ```
74
74
 

3

追記

2021/03/12 06:37

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -86,18 +86,26 @@
86
86
 
87
87
 
88
88
 
89
+ - [repo](https://github.com/teamikl/gentimer)
90
+
91
+
92
+
93
+ 追記: ドキュメント書きかけのままですが、質問と同じような流れで
94
+
95
+ time.sleep での待機 → GUIのイベントループ内では一瞬フリーズする
96
+
97
+ → タイマーを使って解消 → コールバック多用することになりコードが追い難くなる
98
+
99
+ → ジェネレーターを使って解消
100
+
89
101
 
90
102
 
91
103
  ----
92
104
 
93
- Demo
105
+ Demo スペースキーの入力待等も同様に、同じ関数内で処理できます。
94
106
 
95
107
 
96
108
 
97
109
  - [CountDown demo (generator版)](https://repl.it/@MiKLTea/tkGenCountdown#main.py)
98
110
 
99
111
  - [CountDown demo (asyncio版)](https://repl.it/@MiKLTea/tkAsyncGame#main.py)
100
-
101
-
102
-
103
- ※ スペースキーの入力待等も同様に、同じ関数内で処理できます。

2

解説を追記

2021/03/11 12:23

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -48,6 +48,8 @@
48
48
 
49
49
  数行で出来るので、実装方法も紹介。
50
50
 
51
+ (pypi に登録したパッケージでは、処理のキャンセル迄を実装してます。)
52
+
51
53
 
52
54
 
53
55
  ```python
@@ -72,6 +74,20 @@
72
74
 
73
75
 
74
76
 
77
+ 解説: ジェネレーターを使うと yield の部分で処理を中断、
78
+
79
+ 再度 next() で呼ぶと、続きから再開できます。
80
+
81
+
82
+
83
+ この仕組をイベントループのタイマーと組み合わせて、yield で 待機したい秒数を返し、
84
+
85
+ イベントループ側では、指定秒数後に続きの処理を再開~次のyield 迄実行、という風にしてます。
86
+
87
+
88
+
89
+
90
+
75
91
  ----
76
92
 
77
93
  Demo
@@ -81,3 +97,7 @@
81
97
  - [CountDown demo (generator版)](https://repl.it/@MiKLTea/tkGenCountdown#main.py)
82
98
 
83
99
  - [CountDown demo (asyncio版)](https://repl.it/@MiKLTea/tkAsyncGame#main.py)
100
+
101
+
102
+
103
+ ※ スペースキーの入力待等も同様に、同じ関数内で処理できます。

1

サンプルコードを追加

2021/03/11 11:47

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -69,3 +69,15 @@
69
69
  # gen_timer(root, count_down(10))
70
70
 
71
71
  ```
72
+
73
+
74
+
75
+ ----
76
+
77
+ Demo
78
+
79
+
80
+
81
+ - [CountDown demo (generator版)](https://repl.it/@MiKLTea/tkGenCountdown#main.py)
82
+
83
+ - [CountDown demo (asyncio版)](https://repl.it/@MiKLTea/tkAsyncGame#main.py)