Search code examples
python-3.xcx-freeze

How to build python class structure for matplotlib to export ot .exe with cx_freeze?


I built a code to generate and play random musical notes. It is working great within python, but I would like to make it into an .exe stand alone program, so people without python can use it. I show an image below of the output. It creates a matplotlib figure with a 'TkAgg' backend. There are 5 buttons and an user entry box that all work.

I used cx_freeze to try to package it, and I worked through all of the errors. I also got some of the examples to work. I can see the the build folder is getting the 4 Images and many .wav files I need to draw the musical staff and play the notes. One error showed that the .exe tried to run my code, because it couldn't find the .wav files). I changed how I specified where they were for the .exe. But now when I run the .exe nothing happens.

Unfortunately my code is a monstrosity. It's messy, and somewhat long (750 lines if you count white space). The .py file I am trying to write to the .exe is Interval_Trainer_v1_1.py. It can be found here.

Because it works in python, but not in the .exe, I thought it might have to do with my ignorance of how to use classes in conjunction with plotting well. Basically I call the class, and then initialize a bunch of things so I can refer to them later. That allows me to delete notes I've plotted before, old answers, etc.

How can I practice building up 'TkAgg' backended figures that will execute properly after cf_freeze? I feel like I need to start with some basic ideas and build up to my application, which is fairly complex.

One note, I do use pygame for the sounds.

Here is my setup file:

from cx_Freeze import setup, Executable
import os
os.environ['TCL_LIBRARY']=r'C:\Users\Bart\Anaconda3\tcl\tcl8.6'
os.environ['TK_LIBRARY']=r'C:\Users\Bart\Anaconda3\tcl\tk8.6'

import sys
base = None
if sys.platform == 'win32':
    base = 'Win32GUI'
additional_mods = ['numpy.core._methods', 'numpy.lib.format',"matplotlib.backends.backend_tkagg", 'matplotlib.pyplot', 'matplotlib.image', 'matplotlib.widgets']
setup(
    name = "Interval Trainer",
    version = "1.0.0",
    author = "Bart",
    author_email = "[email protected]",
    options = {"build_exe": {'includes': additional_mods,"packages":["pygame","tkinter",'random'],
                         "include_files": [
                         'Images/F cleff 8vb.png', 'Images/F cleff.png',
                         'Images/G cleff 8vb.png', 'Images/G cleff.png',
                         'Pitches/A#1.wav', 'Pitches/A#2.wav', 'Pitches/A#3.wav',
                         'Pitches/A#4.wav', 'Pitches/A#5.wav', 'Pitches/A1.wav',
                         'Pitches/A2.wav', 'Pitches/A3.wav', 'Pitches/A4.wav',
                         'Pitches/A5.wav', 'Pitches/Ab1.wav', 'Pitches/Ab2.wav',
                         'Pitches/Ab3.wav', 'Pitches/Ab4.wav', 'Pitches/B#2.wav',
                         'Pitches/B#3.wav', 'Pitches/B#4.wav', 'Pitches/B1.wav',
                         'Pitches/B2.wav', 'Pitches/B3.wav', 'Pitches/B4.wav',
                         'Pitches/B5.wav', 'Pitches/Bb1.wav', 'Pitches/Bb2.wav',
                         'Pitches/Bb3.wav', 'Pitches/Bb4.wav', 'Pitches/C#2.wav',
                         'Pitches/C#3.wav', 'Pitches/C#4.wav', 'Pitches/C#5.wav',
                         'Pitches/C2.wav', 'Pitches/C3.wav', 'Pitches/C4.wav',
                         'Pitches/C5.wav', 'Pitches/C6.wav', 'Pitches/D#2.wav',
                         'Pitches/D#3.wav', 'Pitches/D#4.wav', 'Pitches/D#5.wav',
                         'Pitches/D2.wav', 'Pitches/D3.wav', 'Pitches/D4.wav',
                         'Pitches/D5.wav', 'Pitches/Db1.wav', 'Pitches/Db2.wav',
                         'Pitches/Db3.wav', 'Pitches/Db4.wav', 'Pitches/E#2.wav',
                         'Pitches/E#3.wav', 'Pitches/E#4.wav', 'Pitches/E1.wav',
                         'Pitches/E2.wav', 'Pitches/E3.wav', 'Pitches/E4.wav',
                         'Pitches/E5.wav', 'Pitches/Eb2.wav', 'Pitches/Eb3.wav',
                         'Pitches/Eb4.wav', 'Pitches/F#1.wav', 'Pitches/F#2.wav',
                         'Pitches/F#3.wav', 'Pitches/F#4.wav', 'Pitches/F#5.wav',
                         'Pitches/F1.wav', 'Pitches/F2.wav', 'Pitches/F3.wav',
                         'Pitches/F4.wav', 'Pitches/F5.wav', 'Pitches/G#1.wav',
                         'Pitches/G#2.wav', 'Pitches/G#3.wav', 'Pitches/G#4.wav',
                         'Pitches/G#5.wav', 'Pitches/G1.wav', 'Pitches/G2.wav',
                         'Pitches/G3.wav', 'Pitches/G4.wav', 'Pitches/G5.wav',
                         'Pitches/Gb1.wav', 'Pitches/Gb2.wav', 'Pitches/Gb3.wav',
                         'Pitches/Gb4.wav']}},
    executables = [Executable("Interval_trainer_v1_1.py", base=base)],
    )

Output Image

Any help is appreciate.


Solution

  • See the matplotlib user interfaces examples embedding_in_tk and embedding_in_tk2 to practice building up TkAgg backended figures.