我有一个简单的kivy应用,我想为其生成一个exe。

我使用virtualenv --python=C:\Python27\python.exe <path/to/new/virtualenv/>生成了一个虚拟环境。然后,我激活该环境。

然后在virtualenv中安装以下模块:

python -m pip install --upgrade pip wheel setuptools

python -m pip install docutils pygments pypiwin32 kivy.deps.sdl2 kivy.deps.glew --extra-index-url https://kivy.org/downloads/packages/simple/

python -m pip install kivy

pip install pyinstaller


然后,我在下面有以下文件。

touch.py文件包含以下代码:

import ctypes
import kivy
kivy.require('1.10.0')
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.core.window import Window

SIZE = 2
NUM_BOXES = SIZE * SIZE

class MyGrid(GridLayout):
    def __init__(self, app, matrix, m_size, **kwargs):
        super(MyGrid, self).__init__(**kwargs)
        self.app = app
        self.matrix = matrix
        self.m_size = m_size
        self.sq_size_x = Window.size[0] / m_size
        self.sq_size_y = Window.size[1] / m_size
        self.count = set()

    def on_touch_down(self, touch):
        pass

    def on_touch_move(self, touch):
        try:
            mouse_x, mouse_y = touch.pos
            section_x = int(mouse_x // self.sq_size_x)
            section_y = int(mouse_y // self.sq_size_y)

            self.matrix[section_y][section_x].background_color = (0, 1, 0, 1)

            self.count.add(str(section_y) + "_" + str(section_x))
        except:
            ## DONT KNOW WHY IT GETS OUT OF RANGE SOMETIMES
            pass

        ## FULLY COLORED, CLOSE APP
        if len(self.count) >= NUM_BOXES:
            self.app.stop()

    def on_touch_up(self, touch):
        pass

class MyApp(App):
    def populate_matrix(self):
        matrix = [ [ Button(text="") for x in range(self.m_size) ] for y in range(self.m_size)]
        return matrix

    def populate_layout(self):
        for y in range(self.m_size-1,-1,-1): ## FILL y BACKWARDS, SINCE BOTTOM IS 0
            for x in range(self.m_size):
                self.layout.add_widget(self.matrix[y][x])

    def get_screen_size(self):
        user32 = ctypes.windll.user32
        screensize = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)

        return screensize

    def build(self):
        screensize = self.get_screen_size()
        Window.size = (screensize[0], screensize[1])
        Window.fullscreen = 'auto'  ## MAKES APP FULLSCREEN BUT HAS COORDINATE ISSUES (IF ONLY BYITSELF)
        Window.borderless = True
        self.m_size = SIZE
        self.matrix = self.populate_matrix()
        self.layout = MyGrid(self, self.matrix, self.m_size, cols=self.m_size)
        self.populate_layout()

        return self.layout

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


touch.spec文件包含以下代码:

# -*- mode: python -*-

block_cipher = None


from kivy_deps import sdl2, glew

a = Analysis(['touch.py'],
             pathex=["C:\\Users\\XXX\\Desktop\\testingKivy"],
             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,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='touch',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )

coll = COLLECT(exe, Tree("C:\\Users\\XXX\\Desktop\\testingKivy"),
   a.binaries,
   a.zipfiles,
   a.datas,
   *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
   strip=False,
   upx=True,
   name='touchtracer')


然后,我运行命令:

python -m PyInstaller --onefile touch.spec


仅执行pyinstaller --onefile touch.py时,生成的exe不起作用。似乎sld2下的某些依赖项未正确打包。因此,我最终使用上述内容更新了.spec文件。

使用touch.spec文件时,它确实会生成一个exe,但是它本身无法正常工作。在dist文件夹下,它会生成一个touch.exe(它本身无法运行),但是在dist文件夹下,它还会生成一个touchtracer文件夹,其中有很多文件/ dll,还有一个touch.exe(效果很好)。我的问题是,有没有办法将其打包为单个exe?

有人确实说过“请确保规范文件中没有收集步骤:

“在单文件模式下,没有调用COLLECT,并且EXE实例接收所有脚本,模块和二进制文件。”(link)。但是我不知道如何以另一种方式包括sdl2依赖项。在这种情况下,不确定如何正确使用--add-data。

最佳答案

使用Pyinstaller文件参数运行.spec使其几乎忽略您在命令行上提供的所有选项。我建议做一个pyi-makespec --onefile touch.py以使.spec文件开始一个文件。然后编辑touch.spec文件以添加sdl内容。只需在*[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)]行之后的exe部分中添加同一行(a.datas)(就像在coll部分中一样)。然后只需运行python -m PyInstaller touch.spec

关于python - 使用pyinstaller为Kivy应用生成单个exe,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56676755/

10-15 12:57