python - 使用 pyinstaller 生成的基于 kivy 的 Windows exe 的黑屏
问题描述
我有一个基于Kivy
and的小型应用程序python
。如果我从 Visual Studio 代码运行它,它工作正常。但是,如果我使用它pyinstaller
生成 exe,生成的 exe 会显示黑屏。
下面是我的 .py 文件:
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()
下面是我的 kivy 文件:
# 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()
下面是 .spec 文件:
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')
我是新来的基维。如果我做错了什么,请告诉我。生成的 .spec 文件稍作修改。默认生成的文件正在生成一个甚至没有启动的 exe。但是在这里使用修改后的 .spec 文件,exe 正在启动,但小部件不可用。
解决方案
黑屏意味着应用程序没有读取 kv 文件中的 UI,因此您需要将其包含在数据列表中的规范文件中
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')
最后你可以运行pyinstaller pyinstaller.spec
,如果你想了解更多关于 pyinstaller 规范的信息,你可以在这里看到这个链接
推荐阅读
- vue.js - v-card 内的导航抽屉在 Vuetify 中不起作用
- python - 一个功能只有一小部分在工作
- react-native - React-Native Flatlist Single Item Selector 在渲染不同的组件时无法更新组件
- android - 如何通过动态创建的按钮在 Fragment 之间导航?
- javascript - (反应)如何将状态的值设置为具有对象的属性的对象?
- java - Java - 与 Jackson 一起编写 Json
- python - 用于创建 sql 查询的 Python 脚本
- flutter - 使用自定义笔画帽在颤动中创建自定义圆形进度指示器
- javascript - 使用嵌入返回消息
- r-markdown - Rmarkdown 路径不存在