Search code examples
pythonpython-2.7tkinterpy2exe

py2exe with different parts of apps


Let's say I have 5 different python files: main.py, first.py, second.py, third.py, last.py

Each of the files, after main.py, does something different through a button on main.py's screen. I know how to use py2exe:

from distutils.core import setup
import py2exe

setup(console=['main.py'])

Now, how would I compile all of these into EXE files such that I could still use the buttons to open the other .py files?


Solution

  • What I didn't realize is, I imported the other files into the main.py file, and they became modules! When you use py2exe, it compiles the modules. These can then be used in the program, for example:

    main.py

    from first '''Python File Name''' import *
    from Tkinter import *
    import tkMessageBox
    
    root = Tk()
    def helloCallBack():
          tkMessageBox.showinfo(hi_function('Joe'))
    
    B = Button(root, text ="Click Me", command = helloCallBack)
    
    B.pack()
    root.mainloop()
    

    first.py

    def hi_function(name):
       return 'Hello %s'%(name)
    

    Python takes first.py and imports it as a module. Py2exe takes the modules in main.py and converts them to PYD files. So, the main thing is, you don't need to modify the code to call the .exe file. Simply, just import the other .py files as modules and py2exe will do the rest.

    I'm going to put this on the community wiki for others to learn from.