基于 kivy windows 使用 pyinstaller 生成的 exe 的黑屏

Black screen for kivy based windows exe generated using pyinstaller

我有一个基于 Kivypython 的小应用程序。如果我 运行 它形成 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')

我是kivy的新手。如果我做错了什么,请告诉我。生成的 .spec 文件稍作修改。默认生成的文件正在生成一个甚至没有启动的 exe。但是这里使用修改后的 .spec 文件,exe 正在启动但小部件不可用。

黑屏表示应用程序未读取 kv 文件中的 UI,因此您需要将其包含在 datas 列表中的 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=[('*.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 规范的更多信息,可以查看此 link here