LoginSignup
8
10

More than 3 years have passed since last update.

[Python] cx_FreezeでTkinterのプログラムの配布ファイルを作成する

Posted at

1. はじめに

Tkinterで作ったGUIプログラムを、配布するときに、exe実行ファイルよりInstallerとして渡したほうがいいと思います。cx_Freezeにその機能があったので、それを試してみました。

今回、Installer化するプログラムはこちらです。(OS:Windows 10, 64bit)

[Python] Tkinterで複数のWindowを表示

file name:multi_winews_tkinter.py

2. cx_Freezeの設置

Anacondaを使っているため、pipではなくcondaで設置します。

conda install -c conda-forge cx_freeze

3. setup.pyファイルの作成

Pythonのプログラム(file name:multi_winews_tkinter.py)が置いてあるフォルダーにsetup.pyを作成します。

setup.pyの書き方は、こちらのページを参考にしました。
様々な用途の、サンプルsetup.pyが容易されているので、参考にしてください。(例:PyQTでGUI作成)
https://github.com/anthony-tuininga/cx_Freeze/tree/master/cx_Freeze/samples

setup.py
import sys
from cx_Freeze import setup, Executable

base = None
if sys.platform == 'win32':
    base = 'Win32GUI'

executables = [
    Executable('multi_windows_tkinter.py', base=base)
]

setup(name='simple_Tkinter',
      version='0.2',
      description='Sample cx_Freeze Tkinter script',
      executables=executables
      )

4. InstallerファイルのBuild

ターミナルで、下記の命令を実行します。

python setup.py bdist_msi

そうなると、Installerファイル(msiファイル)が形成されます。今回のInstallerの容量は、11MBでした。

image.png

5. 別のPCでInstallerを実施

Python環境が設置されていないPCで、上記のInstallerファイルを実行します。
下記の画面が表示され、設置が始まります。

image.png

image.png

image.png

設置結果、問題なく動作しました。
最後に、アンインストールしたい時には、Windows Control Panelで他のWindows Program同様、簡単にアンインストールが可能です。便利ですね。

6. まとめ

  1. cx_FreezeでTkinterプログラムの配布ファイルを作成。
  2. Exe形式ではなく、Installer形式を作成。
  3. Python環境がないパソコンにInstallし、無事に動作していることを確認。

 cx_Freezeいいですね。動作も早いし、生成ファイルのサイズも小さく、満足しています。

参考資料

1.[Python] Tkinterで複数のWindowを表示
2.cx Freeze distutils setup script
3.cx Freeze setup examples

8
10
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
8
10