You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.1 KiB

  1. # Using Custom Images while Packaging
  2. > Note: if you're new to packaging Gooey, checkout the main [Packaging Guide](https://github.com/chriskiehl/Gooey/blob/doc-improvements/docs/packaging/Packaging-Gooey.md) first!
  3. Gooey comes with a set of six default icons. These can be overridden with your own custom images/icons by telling Gooey to search additional directories when initializing. This is done via the `image_dir` argument to the `Gooey` decorator.
  4. ```python
  5. @Gooey(program_name='Custom icon demo', image_dir='/path/to/images')
  6. def main():
  7. # rest of program
  8. ```
  9. While this works for regular executions, a little additional work is required to make sure that your images will actually be available when running as a stand alone executable.
  10. To make your custom images available after packaging, you have to do two things.
  11. **Step 1:** wrap the path to your image directory in the `local_resource_path()` function provided by Gooey. When PyInstaller runs your application, it decompresses all the contents to a random temp directory. This function will handle the logic of resolving that directory and fetching your resources from it.
  12. ```python
  13. from gooey import Gooey, local_resource_path
  14. @Gooey(image_dir=local_resource_path('relative/path/to/images'))
  15. def main():
  16. ...
  17. ```
  18. **Step 2:** Update `build.spec` to include the image directory during bundling. This is done by giving the path to your Images as a Tree object to Pyinstaller's `EXE` section.
  19. ```
  20. # -*- mode: python ; coding: utf-8 -*-
  21. import os
  22. ...
  23. # LOOK AT ME! I AM A TREE OBJECT
  24. image_overrides = Tree('path/to/images', prefix='path/to/images')
  25. ...
  26. exe = EXE(pyz,
  27. a.scripts,
  28. a.binaries,
  29. a.zipfiles,
  30. a.datas,
  31. options,
  32. image_overrides, # <-- NEW
  33. name='APPNAME',
  34. debug=False,
  35. strip=None,
  36. upx=True,
  37. console=False,
  38. icon=os.path.join(gooey_root, 'images', 'program_icon.ico'))
  39. ```
  40. And then build via PyInstaller as usual.
  41. ```
  42. pyinstaller -F --windowed build.spec
  43. ```
  44. PyInstaller will now include your images in its bundle.