Windows

To package our text editor for Windows, we will need to use a module known as cx_freeze. This module is installed using pip via pip install cx_freeze.

To tell cx_freeze about our application, we will need to adjust our setup.py file:

from cx_Freeze import setup, Executable

import sys
base = 'Win32GUI' if sys.platform=='win32' else None

import os
PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')

options = {
'build_exe': {
'include_files':[
os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),
],
},
}

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

setup(name='tkedit',
version = '1.0',
description = 'A tkinter text editor',
options = options,
executables = executables)

Instead of the setup function from distutils, we will be using the cx_freeze version. We import the sys module and use it to set a special variable as if we are on a 32 bit operating system.

Windows needs to know the location of the library of tcl and tk (which power Tkinter). To correctly form the paths to these files, we get the path to our Python installation, then join the necessary folders to it. These are set as environment variables, since cx_freeze will need to reference them during the build process.

When building the .exe, file, cx_freeze will also need to find the .dll files for tk and tcl. We create the paths to them inside a dictionary named options, under the build_exe and include_files keys.

With these libraries located, we now need to create an executable. We create an instance of the Executable class from cx_freeze and pass it a filename of tkedit.py, as well as the base which we defined earlier.

Once that is taken care of, we call the setup function as before, passing it our new options and executables variables.

Before we can run this, we need to create tkedit.py. Luckily, this is a very small file:

from texteditor import main

main()

Much like we had to allude to with Pip's version of the setup.py file, we need to call a function when the executable is run. This function will be the main function from our texteditor.py file.

In this file, we just import the function and run it. This allows cx_freeze to just run the tkedit.py file instead of parsing out the main function from our texteditor.py file.

With these two files taken care of, we can now build our application for windows. Open a command line in your root folder and run the following command:

python3 setup.py build

If everything has gone smoothly, you should see a lot of information about copying libraries, and then find that a folder named build has been added to your project.

Open up explorer and navigate to this folder. You should see another folder beginning with exe-win32. Head to that folder and you should see tkedit.exe sitting there. Run this file and check out your text editor!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset