Reputation: 47
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.
Upvotes: 2
Views: 480
Reputation: 63
I did it this way:
python -m PyInstaller --onefile --name my_app --icon .\assets\my.ico --add-data "main.kv:." --add-data "assets/*.*:assets" main.py
The add-icon was so my app could be shown in the taskbar The first add-data was for UI to show and the second one was for the PNG files I had for images on buttons etc.
I got this from here : https://pyinstaller.org/en/stable/spec-files.htm
but not after many trial and error builds and this piece of code I got on SO to allow me to check what error was being generated.
if __name__ == '__main__':
try:
if hasattr(sys, '_MEIPASS'):
resource_add_path(os.path.join(sys._MEIPASS))
app = MainApp()
app.run()
except Exception as e:
print(e)
input("Press enter.")
NB: This is KIVY app not KIVYMD so I never needed to change the .spec file
Upvotes: 0
Reputation: 728
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
Upvotes: 2