【问题标题】:Black screen for kivy based windows exe generated using pyinstaller使用 pyinstaller 生成的基于 kivy 的 Windows exe 的黑屏
【发布时间】:2021-10-24 13:26:24
【问题描述】:

我有一个基于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 正在启动,但小部件不可用。

【问题讨论】:

    标签: python kivy pyinstaller


    【解决方案1】:

    黑屏表示应用没有读取 kv 文件中的 UI,因此您需要将其包含在数据列表中的 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 规范的更多信息,可以查看此链接 here

    【讨论】:

      猜你喜欢
      • 2016-05-17
      • 1970-01-01
      • 2020-11-17
      • 2019-11-02
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 2020-12-17
      • 1970-01-01
      相关资源
      最近更新 更多