作为代码,它没有任何问题,但是当我将它转换为 exe 时,出现无法获取窗口,中止错误 我尝试了我在论坛中找到的所有解决方案 可以将此代码转换为 exe 的人告诉我我他是怎么做到的?
from kivymd.app import MDApp
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.clock import Clock
from kivymd.uix.textfield import MDTextField
from kivymd.uix.button import MDFloatingActionButton
class main(BoxLayout,MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.orientation = "vertical"
self.a = MDTextField(size_hint_y=.1, hint_text="Write for translate", helper_text="Press the button for translate",helper_text_mode="on_focus", icon_right="cursor-text", size_hint_x=None, width=300,pos_hint={'center_x':0.5,'center_y':0.96})
self.add_widget(self.a)
self.b = MDFloatingActionButton(size_hint_y=.1, icon='translate',pos_hint={'center_x':0.5,'center_y':0.96})
self.add_widget(self.b)
self.b.bind(on_release=self.show_data)
self.layout = BoxLayout(size_hint_y=.8)
self.add_widget(self.layout)
def show_data(self, show):
self.list = self.a.text.split()
for i in range(len(self.list)):
if not self.list[i].endswith(".png"):
self.list[i] += ".png"
print(self.list)
Clock.schedule_once(self.call)
def call(self, call):
self.numb = 0
Clock.schedule_interval(self.do, 0.7)
def do(self, do):
if (self.numb < len(self.list)):
self.layout.clear_widgets()
self.layout.add_widget(Image(source=self.list[self.numb]))
self.numb += 1
else:
self.numb = 0
self.list = [self.a.text]
Clock.unschedule(self.do)
self.layout.clear_widgets()
class app(MDApp):
def build(self):
return main()
if __name__ == "__main__":
app().run()
我试过 Cx_freeze ,卸载 kivy 和其他东西有人可以转换成 exe 吗?
我使用
pyinstaller
成功地将您的代码转换为 exe。这是我使用的 spec
文件(假设您的 python 文件名为 main.py
):
# -*- mode: python ; coding: utf-8 -*-
from kivy_deps import sdl2, glew
block_cipher = None
a = Analysis(['main.py'],
pathex=[],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
hooksconfig={},
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='main',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True,
disable_windowed_traceback=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None )
注意
kivy_deps
包含。
Kivy 需要一些 DLL,你必须帮助 exe 制造商知道这些 DLL 在哪里。
https://kivy.org/doc/stable/guide/packaging-windows.html?highlight=window