tkinkerで生成したアプリを誰でも利用できるようにexeファイルに変換します。
pyinstallerのインストール
pythonのソースコードをexeファイルにするため、pyinstallerを使用します。
pyinstallerはpipコマンドでインストールできます。
$ pip install pyinstaller
exeファイルの作成
実行する際に使用するオプションは以下の通りです。
オプション名 | 詳細 |
---|---|
--onefile | exeファイルのみを生成する |
--name | フォルダやファイル名を変更する |
-noconsole | 実行時にコンソールを表示しない |
テキストとScrollbarのテスト用サンプルプログラムを作成する例です。
sample_text_scrollbar.py
root = tk.Tk()
root.geometry('300x180')
root.title('TextとScrollbarのテスト')
# Textウィジェットの生成と配置
text = tk.Text(root, width=38, height=12)
text.grid(row=0, column=0)
# 垂直方向のScrollbarウィジェットの生成と配置
scroll_v = tk.Scrollbar(root, orient=tk.VERTICAL, command=text.yview)
scroll_v.grid(row=0, column=1, sticky=tk.N+tk.S)
text["yscrollcommand"] = scroll_v.set
root.mainloop()
sample_text_scrollbar.pyをtkinter_sample.exeというファイル名で生成する例です。--onefileを指定していないと実行後に生成されるdistフォルダにexeファイル以外のファイルも生成されます。
--noconsoleを指定していないと、exe実行時にconsoleが起動します。
$ pyinstaller sample_text_scrollbar.py --name tkinter_sample.exe --onefile --noconsole
実行すると「build」フォルダと「dist」フォルダが作成されます。
exeファイルはdistフォルダ内にあります。
サイズは約9MBでした。
tkinter_sample.exeを起動させると、無事起動できました。