3

I made a simple Python3-program which uses PySimpleGUI to create a Windows-GUI-programm. This program contains also a picture CAESAR.png, which is in the same folder as my python-code. Here is the portion of the code for the layout:

import PySimpleGUI as sg


layout = [[sg.Image('CAESAR.png')],
          [sg.Text("Geheime Nachricht in GROSSBUCHSTABEN eintippen:")],
          [sg.Multiline(size=(70,4),key="GEHEIM")],
          [sg.Spin([i for i in range(1,26)], initial_value=12, key="SS"), sg.Text("Schlüssel zwischen 1 und 25 wählen")],
          [sg.Radio("Codieren:", "RADIO1", key="XX" ,default=True),
           sg.Radio("Decodieren:","RADIO1", key="YY")],
          [sg.Text("ERGEBNIS:")],
          [sg.Multiline(size=(70,4),key="AUSGABE")],
          [sg.Button("LOS"), sg.Button("ENDE")]]

window = sg.Window("Cäsars Geheimcode", layout)

This works ok so far. Now I want to make an windows-exe file with pysimplegui-exemaker (version 1.3):

python -m pysimplegui-exemaker.pysimplegui-exemaker

The compiled exe-file runs ok, when the picture CAESAR.png is in the same folder as the exe-file. If the picture is not in the same folder as the exe-file, I get an error-message. Question: How can I force pysimplegui-exemaker to "embed" the picture-file into the exe-file, so that this exe-file runs properly without the extra CAESAR.png in the same folder?

according to the answer and link given by I @BhargavDesai did the following, to get a relativ path:

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
    return os.path.join(base_path, relative_path)


bild = resource_path("CAESAR.png")

Unfortunately the following steps given by the link did not work for me. any hints?

Kurt
  • 264
  • 1
  • 7
  • 23

3 Answers3

2

To solve the issue we need to specifically tell Pyinstaller that we have extra files that need to be "bundled" with the application.

We also need to be using a 'relative' path, so the application can run properly when it's running as a Python Script or a Frozen EXE.

You can find more detailed answer here : Bundling data files with PyInstaller (--onefile)

Bhargav Desai
  • 941
  • 1
  • 5
  • 17
2

First, do this

import base64

with open('CAESAR.png', 'rb') as f:
    print(base64.b64encode(f.read()))

Next copy the output from above and define a variable

CAESAR = b'base64data...'
layout = [[Sg.Image(data=CAESAR)],
...
Elijah
  • 1,814
  • 21
  • 27
1

A much more full-featured program to interface with PyInstaller was released by the PySimpleGUI project a few weeks ago.... psgcompiler. You can pip install it. It's also available under the PySimpleGUI GitHub repo account.

Mike from PSG
  • 5,312
  • 21
  • 39