0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PyInstaller onefileのTk画面を撮る:隠しウィンドウを避けて本体を特定する

0
Posted at

はじめに

PyInstallerの --onefile で作ったGUIアプリを自動でスクリーンショット撮影すると、アプリ画面ではなく**「PyInstaller Onefile Hidden Window」**が選ばれることがあります。ブートローダーの隠しウィンドウと、実際のTkウィンドウは別のプロセス・別のウィンドウだからです。

この記事では、最小のTkアプリをonefileでビルドし、EnumWindows でウィンドウを列挙して本体を特定する方法を実測します。

環境

  • Windows 11(ビルド 10.0.26200)
  • Python 3.11.5
  • PyInstaller 6.19.0
  • Tkinter

やったこと

1. 最小のTkアプリ

# tkapp.py
import tkinter as tk

root = tk.Tk()
root.title("ArticleTestApp v1.0")
root.geometry("420x240")
tk.Label(root, text="hello").pack()
root.mainloop()

2. onefileでビルドする

python -m PyInstaller --onefile --noconsole --name ArticleTestApp tkapp.py

--noconsole を付けると、コンソールウィンドウなしで起動します。

3. 起動してウィンドウを列挙する

import ctypes
import ctypes.wintypes as wt
import subprocess
import time

proc = subprocess.Popen([r"C:\work\dist\ArticleTestApp.exe"])
time.sleep(3)

user32 = ctypes.WinDLL("user32", use_last_error=True)
EnumWindows = user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, wt.HWND, wt.LPARAM)
GetWindowTextW = user32.GetWindowTextW
GetWindowTextLengthW = user32.GetWindowTextLengthW
GetClassNameW = user32.GetClassNameW
GetWindowThreadProcessId = user32.GetWindowThreadProcessId
IsWindowVisible = user32.IsWindowVisible

def callback(hwnd, lparam):
    n = GetWindowTextLengthW(hwnd)
    buf = ctypes.create_unicode_buffer(n + 1)
    GetWindowTextW(hwnd, buf, n + 1)
    cls = ctypes.create_unicode_buffer(256)
    GetClassNameW(hwnd, cls, 256)
    pid = wt.DWORD()
    GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
    if pid.value == proc.pid or "PyInstaller" in buf.value or "Tk" in cls.value:
        print(pid.value, repr(buf.value), cls.value, bool(IsWindowVisible(hwnd)))
    return True

EnumWindows(EnumWindowsProc(callback), 0)

結果:

65028 'ArticleTestApp v1.0' TkTopLevel True
65020 'PyInstaller Onefile Hidden Window' PyInstallerOnefileHiddenWindow False

親プロセス(65020)の隠しウィンドウは非表示、子プロセス(65028)のTkウィンドウは表示されています。

4. 本体を選ぶ条件

  • タイトルがアプリ名と一致する
  • クラスが TkTopLevel(またはアプリ固有のクラス)
  • IsWindowVisible が True
  • 幅・高さが十分にある(例: 100px以上)
  • PyInstallerOnefileHiddenWindow と TtkMonitorWindow を除外する

5. アクティブにせず画面外へ移す

ShowWindowAsync = user32.ShowWindowAsync
SetWindowPos = user32.SetWindowPos
SW_SHOWNOACTIVATE = 4
HWND_TOP = 0
SWP_NOSIZE = 0x0001
SWP_NOACTIVATE = 0x0010

ShowWindowAsync(hwnd, SW_SHOWNOACTIVATE)
SetWindowPos(hwnd, HWND_TOP, -2000, -2000, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE)

画面外に移してから PrintWindow で取得すると、デスクトップ上の他のウィンドウを隠さずに撮影できます。

ハマった点

MainWindowHandle はブートローダーを指すことがある

Process.MainWindowHandle や「最初に見つかったウィンドウ」は、隠しウィンドウを返すことがあります。PIDだけで判断せず、タイトル・クラス・可視状態・サイズを組み合わせます。

PrintWindow の成功は「正しい画像」を保証しない

PrintWindow がTrueを返しても、白い空画像のことがあります。取得後に画像を目視し、アプリのタイトルや内容が写っているか確認します。

後片付けはEXEの正確なパスで

テスト用に起動したプロセスが残ると、次回のビルドや販売用ZIPの作成に影響します。実行ファイルのフルパスを確認して、残存プロセスを終了します。

まとめ

  • PyInstaller onefileは、ブートローダーの隠しウィンドウと本体のTkウィンドウを別プロセスで作る。
  • EnumWindows でPID・タイトル・クラス・可視状態・サイズを列挙する。
  • PyInstallerOnefileHiddenWindow を除外し、アプリ名と一致する TkTopLevel を選ぶ。
  • ShowWindowAsync(SW_SHOWNOACTIVATE) と SetWindowPos で画面外へ移してから撮る。
  • 取得画像の目視と、EXEパス指定の後片付けまで行う。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?