Search code examples
pythonkivypyinstaller

Black screen for kivy based windows exe generated using pyinstaller


I have a small application based on Kivy and python. it is working fine if I run it form Visual studio code. But if I generate exe form it using pyinstaller, the generated exe is showing black screen.

Below is my .py file:

from kivy.app import App
#from kivy.core import text
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty


class Demo(GridLayout):
    name = ObjectProperty(None)
    age = ObjectProperty(None)


    def on_click(self):
        print("My name is {} and my age is {}".format(self.name.text, self.age.text))
        self.name.text = ""
        self.age.text = ""

class DemoClassApp(App):

    def build(self):
        return Demo()

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

Below is my kivy file :

# Filename: democlass.kv
<Demo>:
    #cons: 2
    rows: 5
    #row_default_height: 40
    size: root.width, root.height
    name : name
    age : age

    Label:

        text: "Enter your Name"
        font_size: 50

    TextInput:
        id : name
        text: ""
        font_size: 50

    Label:

        text: "Enter your Age"
        font_size: 50

    TextInput:
        id : age
        text: ""
        font_size: 50

    Button:
        text: "submit"
        on_press : root.on_click()

Below is the .spec file:

from kivy_deps import sdl2, glew

# -*- mode: python ; coding: utf-8 -*-


block_cipher = None


a = Analysis(['app1.py'],
             pathex=['C:\\Users\\sj3kc0\\Desktop\\kivy'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)

exe = EXE(pyz,
          a.scripts, 
          [],
          exclude_binaries=True,
          name='app1',
          debug=True,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True)   
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
               strip=False,
               upx=True,
               upx_exclude=[],
               name='app1')

I am new to kivy. let me know if I am doing anything wrong. The ,spec file generated is modified little bit. The default generated file is producing an exe which is not even lauchning. But here with the modified .spec file, the exe is launching but the widgets are not available.


Solution

  • the black screen that's mean the app does not read the UI in the kv file so you need to include it in the spec file inside datas list

    from kivy_deps import sdl2, glew
    
    # -*- mode: python ; coding: utf-8 -*-
    
    
    block_cipher = None
    
    
    a = Analysis(['app1.py'],
                 pathex=['C:\\Users\\sj3kc0\\Desktop\\kivy'],
                 binaries=[],
                 datas=[('*.kv':'.')],# here we add all the kv files are placed in the same app1.py file level assumed that your kv file is
                 hiddenimports=[],
                 hookspath=[],
                 runtime_hooks=[],
                 excludes=[],
                 win_no_prefer_redirects=False,
                 win_private_assemblies=False,
                 cipher=block_cipher,
                 noarchive=False)
    pyz = PYZ(a.pure, a.zipped_data,
                 cipher=block_cipher)
    
    exe = EXE(pyz,
              a.scripts, 
              [],
              exclude_binaries=True,
              name='app1',
              debug=True,
              bootloader_ignore_signals=False,
              strip=False,
              upx=True,
              console=True)   
    coll = COLLECT(exe,
                   a.binaries,
                   a.zipfiles,
                   a.datas,
                   *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
                   strip=False,
                   upx=True,
                   upx_exclude=[],
                   name='app1')
    

    finally you can run pyinstaller pyinstaller.spec and if you like to more information about pyinstaller spec you can see this link here