Differences between revisions 1 and 2
Revision 1 as of 2005-10-25 19:20:37
Size: 1314
Editor: h-66-134-92-2
Comment:
Revision 2 as of 2005-10-28 12:52:56
Size: 3296
Editor: 195
Comment:
Deletions are marked like this. Additions are marked like this.
Line 45: Line 45:

= Alternative setup.py for hello.py =
Another more ellaborated '''setup.py''', without hard-coded versions of libraries and working with the ''bundle_files'' option (dlls for tcl, tk and tix must stand in the exec dir):

{{{
#!python
from distutils.core import setup
import py2exe
from py2exe.build_exe import py2exe as BuildExe
import os,sys

def TixInfo():
    import Tkinter
    import _tkinter
    
    tk=_tkinter.create()
   
    tcl_version=_tkinter.TCL_VERSION
    tk_version=_tkinter.TK_VERSION
    tix_version=tk.call("package","version","Tix")
  
    tcl_dir=tk.call("info","library")
          
    del tk, _tkinter, Tkinter
    
    return (tcl_version,tk_version,tix_version,tcl_dir)

class myPy2Exe(BuildExe):
    
    def plat_finalize(self, modules, py_files, extensions, dlls):
        BuildExe.plat_finalize(self, modules, py_files, extensions, dlls)
        
        if "Tix" in modules:
            # Tix adjustments
            tcl_version,tk_version,tix_version,tcl_dir = TixInfo()
            
            tixdll="tix%s%s.dll"% (tix_version.replace(".",""),
                                    tcl_version.replace(".",""))
            tcldll="tcl%s.dll"%tcl_version.replace(".","")
            tkdll="tk%s.dll"%tk_version.replace(".","")

            dlls.add(os.path.join(sys.prefix,"DLLs",tixdll))
            
            self.dlls_in_exedir.extend( [tcldll,tkdll,tixdll ] )

            tcl_src_dir = os.path.split(tcl_dir)[0]
            tcl_dst_dir = os.path.join(self.lib_dir, "tcl")
            self.announce("Copying TIX files from %s..." % tcl_src_dir)
            self.copy_tree(os.path.join(tcl_src_dir, "tix%s" % tix_version),
                           os.path.join(tcl_dst_dir, "tix%s" % tix_version))

opts={
    'py2exe':{
        'bundle_files':1
    }
}

setup(
    script_args=['py2exe'],
    cmdclass={'py2exe':myPy2Exe},
    windows=['hello.py'],
    options=opts,
)
}}}

Introduction

[http://tix.sourceforge.net/ Tix], the Tk Interface eXtension, is a powerful set of user interface components that expands the capabilities of your Tcl/Tk and Python applications. This page describes building a "Hello World" Tix application with py2exe.

Tix Hello World

Here is a very simple Tix application. Save it as hello.py:

   1 import Tix
   2 
   3 window = Tix.Tk()
   4 Tix.Label(window, text='Hello World').grid()
   5 window.mainloop()

setup.py for hello.py

py2exe doesn't discover the use of the Tix DLL and Tcl files, so you must include them explicitly. The following setup.py builds a working EXE for the hello.py above:

   1 import glob
   2 import os
   3 import sys
   4 from distutils.core import setup
   5 import py2exe
   6 
   7 def files(folder):
   8     for path in glob.glob(folder+'/*'):
   9         if os.path.isfile(path):
  10             yield path
  11 
  12 data_files=[
  13             ('.', glob.glob(sys.prefix+'/DLLs/tix81*.dll')),
  14             ('tcl/tix8.1', files(sys.prefix+'/tcl/tix8.1')),
  15             ('tcl/tix8.1/bitmaps', files(sys.prefix+'/tcl/tix8.1/bitmaps')),
  16             ('tcl/tix8.1/pref', files(sys.prefix+'/tcl/tix8.1/pref')),
  17            ]
  18 
  19 setup(
  20       script_args=['py2exe'],
  21       windows=['hello.py'],
  22       data_files=data_files,
  23      )

Alternative setup.py for hello.py

Another more ellaborated setup.py, without hard-coded versions of libraries and working with the bundle_files option (dlls for tcl, tk and tix must stand in the exec dir):

   1 from distutils.core import setup
   2 import py2exe
   3 from py2exe.build_exe import py2exe as BuildExe
   4 import os,sys
   5 
   6 def TixInfo():
   7     import Tkinter
   8     import _tkinter
   9     
  10     tk=_tkinter.create()
  11    
  12     tcl_version=_tkinter.TCL_VERSION
  13     tk_version=_tkinter.TK_VERSION
  14     tix_version=tk.call("package","version","Tix")
  15   
  16     tcl_dir=tk.call("info","library")
  17           
  18     del tk, _tkinter, Tkinter
  19     
  20     return (tcl_version,tk_version,tix_version,tcl_dir)
  21 
  22 class myPy2Exe(BuildExe):
  23     
  24     def plat_finalize(self, modules, py_files, extensions, dlls):
  25         BuildExe.plat_finalize(self, modules, py_files, extensions, dlls)
  26         
  27         if "Tix" in modules:
  28             # Tix adjustments
  29             tcl_version,tk_version,tix_version,tcl_dir = TixInfo()
  30             
  31             tixdll="tix%s%s.dll"% (tix_version.replace(".",""),
  32                                     tcl_version.replace(".",""))
  33             tcldll="tcl%s.dll"%tcl_version.replace(".","")
  34             tkdll="tk%s.dll"%tk_version.replace(".","")
  35 
  36             dlls.add(os.path.join(sys.prefix,"DLLs",tixdll))
  37             
  38             self.dlls_in_exedir.extend( [tcldll,tkdll,tixdll ] )
  39 
  40             tcl_src_dir = os.path.split(tcl_dir)[0]
  41             tcl_dst_dir = os.path.join(self.lib_dir, "tcl")
  42             self.announce("Copying TIX files from %s..." % tcl_src_dir)
  43             self.copy_tree(os.path.join(tcl_src_dir, "tix%s" % tix_version),
  44                            os.path.join(tcl_dst_dir, "tix%s" % tix_version))
  45 
  46 opts={
  47     'py2exe':{
  48         'bundle_files':1
  49     }
  50 }
  51 
  52 setup(
  53     script_args=['py2exe'],
  54     cmdclass={'py2exe':myPy2Exe},
  55     windows=['hello.py'],
  56     options=opts,
  57 )

TixSetup (last edited 2008-07-08 11:27:43 by localhost)