0
0

Pythonで作ったExeファイルの起動時に、起動中、と表示したい

Posted at

結論

Windows環境であれば、pyinstallerのsplashを利用することで実現できます。

(この記事を書いている2024/06/24時点で)
※公式にexperimentalの記載がありますので、試験的な機能であることをご承知おきください。
※macOSは非対応とのことです。

背景

Pythonで作ったExeファイルの起動時に、起動中と表示したい、と思ったものの、なかなか見つけることができなかったので、記事にしてみました。

もう少し細かい背景

業務上、Pythonで作ったスクリプトをPyinstallerを利用してexe化して配布することが頻繁にあります。
作成したexeの起動に数秒を要し、ユーザー側で起動しているかわかりにくい状況に陥ることがありました。

環境

Windows 10
Python 3.11
Pyinstaller 6.4.0

サンプルコード

以下のように、exeの時にimport pyi_splashで読み込むように指定します。
そして、起動後にpyi_splash.close()で閉じます。

# sample.py
import sys
import tkinter as tk

if getattr(sys, 'frozen', False):
    import pyi_splash

class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.messege_label = tk.Label(self, text='hello world')
        self.messege_label.pack()

if __name__ == '__main__':
    app = App()
    if getattr(sys, 'frozen', False):
        pyi_splash.close()
    app.mainloop()

上記のコードを保存したら、コマンドプロンプトから以下のように入力して、exeファイルを作成します。
※splash-sample.pngはテスト用につけたファイル名です。任意のファイル名を付けてください。

pyinstaller sample.py --onefile --splash splash-sample.png

公式の情報については、以下のリンクから公式のドキュメントをご参照ください。
Pyinstaller

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