GUI (WxPython) executable file (pyInstaller) [Windows] in Python3

GUI (WxPython) executable file (pyInstaller) in Python [Windows]

We will describe a series of methods to generate a GUI display executable file using Python. I used wxPython in the GUI library because it was easy to use as a license.

Root environment

I am using Anaconda for the root environment. Since it is actually used in a virtual environment, I think that it is probably okay in other Conda-based environments.

Anaconda https://www.continuum.io/downloads

Virtual environment

Build a virtual environment

This time I want to run it in a test, so I will build a new virtual environment with the name wxenv without using the root environment. Run the following at the command prompt (Terminal on Mac) Note: As of May 2017, PyInstaller only supports Python 3.5.

command


conda create -n wxenv python=3.5.3
#When creating with a set of Anaconda
# conda create -n wxenv python=3.5.3 anaconda
#When checking the created environment
# conda info -e
# conda list -n wxenv
#When deleting the created virtual environment
# conda remove -n wxenv --all

Virtual environment activation

Subsequent work is performed in the virtual environment.

command


# Windows
activate wxenv
#Deactivate
# deactivate

# Mac
source activate wxenv
#Deactivate
# source deactivate

WxPython

Install WxPython

https://wxpython.org/

Basically, wxPython seems to be Python2 series, but I use wxPython that works with 3 series called Phoenix version. It is said that it supports PyPI from April 2017, and it seems that it can be installed by executing the following command.

command


pip install wxpython

Test run WxPython

Create a test executable file in a suitable location as shown below and save it.

main.py


#! env python
# -*- coding: utf-8 -*-

import wx
import sys,os

if __name__ == '__main__':
    app = wx.App(False)
    frame = wx.Frame(None, wx.ID_ANY, "Hello World")
    frame.Show(True)
    app.MainLoop()

PyInstaller

Installation

command


pip install pyinstaller

Windows For windows, also install the following

windows


pip install pypiwin32

Try it out

First, output from the normal command prompt.

command


#Move to the save location of the test executable file generated earlier
cd (File storage location)
#Execute PyInstaller (1 directory, 1 file as an argument, an instruction to hide the console and erase the previous output result)

pyinstaller main.py -D -F -w --clean

When you run The generated exe file should have been generated in a folder called dist.

spec file

It is generated even if you run pyinstaller normally, but you may want to make fine adjustments at the time of generation. In that case, you can create a spec file, rewrite the file to a convenient setting, and then have pyinstaller read the setting and output it.

https://pythonhosted.org/PyInstaller/spec-files.html https://pythonhosted.org/PyInstaller/usage.html#options

command


#Move to the location of the test executable (unnecessary if already moved)
cd (File storage location)
#Generate spec file
#Target file name, spec file name to be generated, 1 directory, 1 file as an option, console hidden
pyi-makespec main.py -n="main" -D -F -w

Doing the above will generate a file called main.spec. In this file, there are each setting used by PyInstaller, which can be modified to specify an external file. If you cannot generate it well here, try deleting the part (-F) of one file. Also, some external libraries may need to be noted in the spec file. For details, see Official around Adding Binary Files.

command


#Specify a spec file and execute
pyinstaller main.spec

You should now have an exe file in a directory called dist.

About the working directory of PyInstaller

When executing with PyInstaller, it is convenient to write the following contents in a Python file and make the directory containing the executable file the working directory.

main.py


os.chdir(os.path.dirname(os.path.abspath(sys.argv[0])))

Caution

For Mac, wxPython doesn't seem to work unless I use pythonw instead of python. http://www.sevencredit.com/2015/07/31/702/

Matplotlib

Even if Matplotlib is registered in PyInstaller, it often does not work with Qt links as it is. As a workaround, a different backend should work immediately after ʻimport matplotlib`.

import matplotlib
matplotlib.use('TkAgg')
#The following is omitted

https://github.com/pyinstaller/pyinstaller/issues/2857 http://chick.g.hatena.ne.jp/allegro/20091009/p3

WxFormBuilder

In my case, WxPython editing uses wxFormBuilder. https://qiita.com/mm_sys/items/716cb159ea8c9e634300

スクリーンショット 2018-02-16 15.01.02.png

Another way

There was a way to operate lighter and faster than PyInstaller, so I created a separate article.

https://qiita.com/mm_sys/items/1fd3a50a930dac3db299

Recommended Posts

GUI (WxPython) executable file (pyInstaller) [Windows] in Python3
File operations in Python
File processing in Python
File operations in Python
[GUI in Python] PyQt5-Dialog-
Python executable file conversion library comparison (py2exe, cx_freeze, pyinstaller)
Download the file in Python
[GUI in Python] PyQt5 -Widget-
Introducing GUI: PyQt5 in Python
Python install in 2 lines @Windows
[GUI in Python] PyQt5 -Event-
Create an exe file that works in a Windows environment without Python with PyInstaller
Create an executable file (EXE) by PyInstaller in a hybrid environment (Nimporter) of Python + Nim
GUI programming in Python using Appjar
File / folder path manipulation in Python
Hit a command in Python (Windows)
Put MeCab in "Windows 10; Python3.5 (64bit)"
Linebot creation & file sharing in Python
Create a binary file in Python
Windows10: Install MeCab library in python
Python executable file conversion module comparison 2
Python CGI file created on Windows
ORC, Parquet file operations in Python
[GUI in Python] PyQt5-Menu and Toolbar-
GUI creation in python using tkinter 2
Exclusive control with lock file in Python
GUI creation in python using tkinter part 1
Using venv in Windows + Docker environment [Python]
Create a simple GUI app in Python
[Python] [Windows] Serial communication in Python using DLL
[Python] [Windows] Take a screen capture in Python
Write O_SYNC file in C and Python
Creating an exe file with Python PyInstaller: PC freezes in parallel processing
Read the file line by line in Python
Read the file line by line in Python
[GPS] Create a kml file in Python
Python garbled in Windows + Git Bash environment
[Python] Read the specified line in the file
Create a GIF file using Pillow in Python
File DL, byte value and delete in Python3
Read a file containing garbled lines in Python
Create a GUI executable file created with tkinter
Create an executable file in a scripting language
From file to graph drawing in Python. Elementary elementary
[Python] Implemented automation in excel file copying work
Install Python 3.5.1 + numpy + scipy + α in Windows environment
Develop Windows apps with Python 3 + Tkinter (exe file)
Pharmaceutical company researchers summarized file scanning in Python
Trap trapped when running a Python Windows executable
Create a MIDI file in Python using pretty_midi
Standard .py file used in Python trials (template)-2020
Open a file dialog with a python GUI (tkinter.filedialog)
Sample to put Python Kivy in one file
Speed evaluation of CSV file output in Python
Quadtree in Python --2
Python in optimization
CURL in python
Script python file
Metaprogramming in Python
Python 3.3 in Anaconda
SendKeys in Python