回答編集履歴
7
add main関数の呼び出し
answer
CHANGED
@@ -16,6 +16,9 @@
|
|
16
16
|
|
17
17
|
# ウィンドウが閉じられないように、キー入力があるまで待機
|
18
18
|
input()
|
19
|
+
|
20
|
+
if __name__ == "__main__":
|
21
|
+
main()
|
19
22
|
```
|
20
23
|
|
21
24
|

|
6
説明補足を追記
answer
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
IDE等で取られてる手法ですが、sys.stdout を差し替えて実現します。
|
2
2
|
といっても、簡単に出来るという訳ではないので、末尾にデモを添付します。
|
3
3
|
|
4
|
+
(説明補足: 出力先の変更自体は sys.stdou 差し替えと簡単なのですが、
|
5
|
+
プログラムの内容次第では、他に少し工夫が必要になる事があります)
|
6
|
+
|
4
7
|
```
|
5
8
|
@tk_redirect_stdout()
|
6
9
|
def main():
|
5
書式の改善
answer
CHANGED
@@ -28,7 +28,8 @@
|
|
28
28
|
|
29
29
|
デモ用にリダイレクトの例として書きましたが、
|
30
30
|
デバッグの際に面倒になる等の欠点があるので、常用には向きません。
|
31
|
-
Python対応の**EditorやIDE(総合開発環境)の利用をお勧めします。**
|
31
|
+
Python対応の**EditorやIDE(総合開発環境)の利用をお勧めします。**
|
32
|
+
フォントサイズ等自由に設定できるはずです。
|
32
33
|
|
33
34
|
tk_redirect_stdoutの定義は以下。
|
34
35
|
関数のデコレーターもしくは、withでコンテキストマネージャとしても使えます。
|
4
要点を太字に修正
answer
CHANGED
@@ -28,7 +28,7 @@
|
|
28
28
|
|
29
29
|
デモ用にリダイレクトの例として書きましたが、
|
30
30
|
デバッグの際に面倒になる等の欠点があるので、常用には向きません。
|
31
|
-
IDE(開発環境)の利用をお勧めします。フォントサイズ等自由に設定できるはずです。
|
31
|
+
Python対応の**EditorやIDE(総合開発環境)の利用をお勧めします。**フォントサイズ等自由に設定できるはずです。
|
32
32
|
|
33
33
|
tk_redirect_stdoutの定義は以下。
|
34
34
|
関数のデコレーターもしくは、withでコンテキストマネージャとしても使えます。
|
3
コードレイアウト整理
answer
CHANGED
@@ -35,12 +35,12 @@
|
|
35
35
|
※ コンテキストを抜けるとウィンドウは閉じてしまうので、その点だけ注意。
|
36
36
|
|
37
37
|
```python
|
38
|
-
from contextlib import ContextDecorator
|
39
|
-
from multiprocessing import Process, Queue
|
40
38
|
|
41
39
|
import sys
|
42
40
|
from enum import Enum
|
43
41
|
from types import SimpleNamespace
|
42
|
+
from contextlib import ContextDecorator
|
43
|
+
from multiprocessing import Process, Queue
|
44
44
|
|
45
45
|
class Message(Enum):
|
46
46
|
LOG, CLEAR = range(2)
|
@@ -68,8 +68,10 @@
|
|
68
68
|
elif msg.type == Message.CLEAR:
|
69
69
|
text.delete("1.0", tk.END)
|
70
70
|
root.after_idle(root.destroy)
|
71
|
+
|
71
72
|
thread = Thread(target=read_queue_loop, args=(text,queue), daemon=True)
|
72
73
|
thread.start()
|
74
|
+
|
73
75
|
root.title("Output Window")
|
74
76
|
root.geometry("600x400+10+10")
|
75
77
|
root.mainloop()
|
2
コード修正
answer
CHANGED
@@ -47,7 +47,6 @@
|
|
47
47
|
|
48
48
|
def tk_logging_loop(queue):
|
49
49
|
from threading import Thread
|
50
|
-
from queue import Queue
|
51
50
|
import tkinter as tk
|
52
51
|
from tkinter.scrolledtext import ScrolledText
|
53
52
|
|
1
loggingモジュールのリンクを追加
answer
CHANGED
@@ -17,7 +17,7 @@
|
|
17
17
|
|
18
18
|

|
19
19
|
|
20
|
-
他には、printの代わりにlogging を用いると、
|
20
|
+
他には、printの代わりに[logging モジュール](https://docs.python.org/ja/3/library/logging.html) を用いると、
|
21
21
|
ファイルに記録やネットワーク経由でログをやり取り出来たり、
|
22
22
|
自由度の高い出力方法が提供されてます。
|
23
23
|
(枠組みだけなので、自分で実装するか、外部ライブラリを使うことになります)
|