使用pyinstaller为kivy应用程序生成单个exe

2024-04-26 15:11:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的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文件。你知道吗

当使用触摸屏规格文件,它确实生成一个exe,但它本身不起作用。在dist文件夹下,它生成一个触摸.exe(它本身不起作用),但在dist folder下,它还生成一个文件夹touchtracer,其中包含许多文件/dll和触摸.exe(效果很好)。我的问题是,有没有办法把它打包成一个exe?你知道吗

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

“在一个文件模式下,没有要收集的调用,EXE实例接收所有脚本、模块和二进制文件。”“(link)。但是我不知道如何以另一种方式包含sdl2依赖项。不确定如何使用--在本例中正确添加数据。你知道吗


Tags: pip文件fromimportselffalseappsize
1条回答
网友
1楼 · 发布于 2024-04-26 15:11:18

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

相关问题 更多 >