python - Pyinstaller 难以通过 Kivy 构建 FileChooserListView
问题描述
我正在尝试集成 kivy 中包含的文件选择器模块,以允许用户通过 FileChooserListView 获取输入文件的文件字符串,但是当通过 pyinstaller 构建应用程序时,应用程序无法打开。有人碰巧知道问题是什么吗?这是一个示例代码:在 pycharm 中运行良好,但在 pyinstaller 构建时无法打开。
from os.path import exists
from threading import Thread
from sys import exit
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.lang import Builder
from os.path import sep, expanduser, dirname, splitext
from kivy.uix.filechooser import FileChooserListView
KV = '''
<MetaLevel>:
rows: 2
cols: 1
Label:
text: 'test text'
Button:
text: 'test button'
on_press: root.popup()
<file_popup>:
file_chooser: file_chooser
GridLayout:
rows:1
cols:1
FileChooserListView:
id: file_chooser
path: r'C:\\Users'
on_submit: root.printer()
'''
Builder.load_string(KV)
class MetaLevel(GridLayout):
def popup(self):
App.get_running_app().file_popup.open()
class file_popup(Popup):
def printer(self):
print(self.file_chooser.path)
App.get_running_app().file_popup.dismiss()
class Cruncher(App):
def build(self):
self.file_popup = file_popup()
return MetaLevel()
if __name__ == "__main__":
Cruncher().run()
解决方案
检查以确保您已安装库:
pip install docutils pygments pypiwin32 kivy.deps.sdl2 kivy.deps.glew
经过一番挖掘后,我确实让您的脚本成功构建,win32file
特别win32timezone
是一些隐藏的导入。
setup:
C:/
..Temp/
....Test/
......test.py <- 你发布的代码
......TEST.spec
TEST.spec
文件:
from kivy_deps import sdl2, glew
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['C:/Temp/Test/test.py'],
binaries=[],
datas=[],
hiddenimports=['win32file','win32timezone'],
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='TEST',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=False)
coll = COLLECT(exe, Tree('C:\\Temp\\Test\\'),
a.binaries,
a.zipfiles,
a.datas,
*[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
strip=False,
upx=True,
name='TEST')
然后运行pyinstaller C:/Temp/Test/TEST.spec
如果你想要一个--onefile
(我通常这样做):
TEST.spec
文件:
from kivy_deps import sdl2, glew
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['C:/Temp/Test/test.py'],
binaries=[],
datas=[],
hiddenimports=['win32file','win32timezone'],
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,
*[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
[],
name='TEST',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
runtime_tmpdir=None,
console=False)
推荐阅读
- python-3.x - openai健身房包厢空间配置
- python-3.x - 在 Conda 中导入 PySpin:找不到 mkl_intel_thread.dll
- r - 我在将 csv 从 Dropbox 加载到 RStudio Cloud 时遇到问题。你能检查一下我做错了什么吗?
- javascript - 在数组中查找时间,如果没有找到,选择上一个
- javascript - 迭代对象值
- python - 如何在屏幕中更新标签的文本 - Kivy
- python - 如何让一个函数在不定义的情况下什么都不做?
- vue.js - 将 Vue 方法拆分为多个文件?
- java - 配置spring security后未授权调用
- javascript - 如何配置react项目根据父文件夹的名称解析入口文件,而不是默认为index.js?