teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

4

コード修正

2020/09/18 11:41

投稿

teamikl
teamikl

スコア8817

answer CHANGED
@@ -23,8 +23,8 @@
23
23
 
24
24
  # NOTE: for windows only
25
25
  import ctypes
26
- kernel32 = ctypes.WinDLL('kernel32')
26
+ kernel32 = ctypes.windll.kernel32
27
- user32 = ctypes.WinDLL('user32')
27
+ user32 = ctypes.windll.user32
28
28
 
29
29
 
30
30
  def run_program(executor):

3

訂正: プロセスの終了を待つ → スレッドの終了を待つ(スレッド内で別プロセスを起動してるので間違いではないが、コードはThreadPoolなので正確ではない)

2020/09/18 11:41

投稿

teamikl
teamikl

スコア8817

answer CHANGED
@@ -72,7 +72,7 @@
72
72
  button3.grid(row=0, column=2)
73
73
  root.mainloop()
74
74
 
75
- # 外部プロセスの了を待つ
75
+ # スレッドプール内で起動している外部プロセスの了を待つ
76
76
  executor.shutdown()
77
77
 
78
78
 

2

コード変更: executorをローカル変数に移動(main()内で後始末をしていたので、main()内で初期化するように移動)

2020/09/18 08:26

投稿

teamikl
teamikl

スコア8817

answer CHANGED
@@ -26,12 +26,8 @@
26
26
  kernel32 = ctypes.WinDLL('kernel32')
27
27
  user32 = ctypes.WinDLL('user32')
28
28
 
29
- executor = ThreadPoolExecutor(thread_name_prefix="worker")
30
29
 
31
-
32
- def run_program(executor=executor):
30
+ def run_program(executor):
33
- # py main.py で起動していた場合は、元のコンソールに出力されます
34
- # pyw main.py で起動していた場合は、新たなコンソールを開きます
35
31
  def proc():
36
32
  logging.info("call external program")
37
33
  subprocess.call("py -3.8 test.py")
@@ -41,7 +37,6 @@
41
37
 
42
38
  future = executor.submit(proc)
43
39
  future.add_done_callback(done)
44
- logging.debug(future)
45
40
 
46
41
 
47
42
  def set_console_visible(show):
@@ -65,8 +60,11 @@
65
60
  import atexit
66
61
  atexit.register(show_console)
67
62
 
63
+ executor = ThreadPoolExecutor(thread_name_prefix="worker")
64
+ run_command = partial(run_program, executor)
65
+
68
66
  root = tk.Tk()
69
- button1 = tk.Button(root, text="Run", command=run_program)
67
+ button1 = tk.Button(root, text="Run", command=run_command)
70
68
  button2 = tk.Button(root, text="Show", command=show_console)
71
69
  button3 = tk.Button(root, text="Hide", command=hide_console)
72
70
  button1.grid(row=0, column=0)

1

説明補足

2020/09/18 08:17

投稿

teamikl
teamikl

スコア8817

answer CHANGED
@@ -3,6 +3,11 @@
3
3
  それ以外(ソフトを起動したコンソールの表示・非表示)なら
4
4
  プラットフォーム依存のAPIを呼び出す事になります。
5
5
 
6
+ 通常はコンソールの表示・非表示切り替えのようなことはあまりしません。
7
+ メインの処理が単体の独立した実行ファイルで、
8
+ 後付けのGUIから呼び出されて一時的にコンソールが表示されるという構成は、
9
+ 他でも見かけたことがあります。
10
+
6
11
  ----
7
12
 
8
13
  main.py