Search code examples
pythonkivypyinstaller

How to include image in Kivy application with onefile mode pyinstaller?


I have been trying for the past 8 hours to get my image to show up in my .exe built using pyinstaller. My program is written in kivy. Everything else works, but the image doesn't show up. I have read [this][2] question. That has not worked for me. Or I just followed the answer wrong. If someone can please explain to me how to do it step by step, I would be very grateful. Also, if there is another answer, please consider telling me that as well.

I will include my main.py file:

#!/usr/bin/env python3

import os
import sys
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.slider import Slider

class SayHello(App):
    def build(self):
        self.window = GridLayout()
        self.window.cols = 1
        self.window.size_hint = (0.6,0.7)
        self.window.pos_hint = {"center_x": 0.5, "center_y": 0.5}

        self.window.add_widget(Image(source = "say.png"))

        self.greeting = Label(
                        text = "Hello",
                        font_size = 40,
                        color = "#00FFCE"
                        )
        self.window.add_widget(self.greeting)
    
        self.slid = Slider(min = 0, max = 300, orientation = "horizontal")
        self.window.add_widget(self.slid)
    
    
        
    
        
        self.user = TextInput(
                    multiline = False,
                    padding_y = (20,20),
                    size_hint = (1, 0.4)
                    )
        self.window.add_widget(self.user)

        self.button = Button(
                      text = "Click me to greet you!",
                      size_hint = (1,0.5),
                      bold = True,
                      background_color = "00FFCE",
                      )
        
        self.button.bind(on_press = self.callback)
        self.window.add_widget(self.button)

        return self.window


    def slided(self, event):
        self.slid
    
    
    
    def callback(self, event):
        self.greeting.text = "Hello " + self.user.text + "!"



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)

path = resource_path("say.png")
        





    

if __name__ == "__main__":
    SayHello().run()  


  [1]: https://stackoverflow.com/questions/31836104/pyinstaller-and-onefile-how-to-include-an-image-in-the-exe-file
  [2]: https://stackoverflow.com/questions/48467917/include-kv-json-files-while-packaing-kivy-with-pyinstaller-onefile

Thanks in advance!


Solution

  • The problem is with your attempt to fix the 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)
    
    path = resource_path("say.png")
    

    just replace that code with:

    import kivy.resources
    
    if getattr(sys, 'frozen', False):
        # this is a Pyinstaller bundle
        kivy.resources.resource_add_path(sys._MEIPASS)