我有一个名为 calculatrice.py 的 .py 文件和另一个名为 calco.kv 的 .kv 文件。我正在尝试使用 pyinstaller 生成 .exe。尽管我按照 kivy“编程指南“创建 Windows 程序包”文档中的说明进行操作,但 .exe 产品无法运行,因为它找不到 calco.kv 文件。
calculatrice.py:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.config import Config
from kivy.lang import Builder
from math import*
Config.set('graphics', 'width', '450')
Config.set('graphics', 'height', '400')
Builder.load_file('calco.kv')
class Calco(BoxLayout):
"""docstring for Calco"""
def __init__(self, **kwarg):
super(Calco, self).__init__(**kwarg)
#effectuer des operations mathematiques
def result(self):
contain = self.ids.text_id.text #stockage du contenu du textinput
liste= [] #initialisation de la liste
indice = len(contain) #trouver la taille de la chaine de caractere pour les iterations
a = ''
b = ''
try: #essayer ces instructions
for i in range (indice):
liste.append(contain[i]) #remplissage de la liste des caracteres du de la chaine de caractere contain
for n in range (len(liste)):
if liste[n]=='²':
a= liste[n-1]+'*'+liste[n-1] #operation pour calculer le carré
liste[n-1]= str(eval(a)) #remplacement de nombre par son carré
for i in liste:
if i=='²':
pass
else:
b += i
self.ids.text_id.text = str(eval(b))
except :
self.ids.text_id.text = 'Error'
#calcul du discriminant
def discriminant(self, polynome):
liste = []
self.a = ''
self.b = ''
self.c = ''
for word in polynome:
liste.append(word)
operation = ['-', '+']
try:
if liste[0] not in operation:
for i in range(len(liste)):
if liste[i]=='-' or liste[i]=='+':
liste_1 = liste[0:i]
if '²' in liste_1:
liste_1.remove('X')
liste_1.remove('²')
if liste_1 == []:
self.a='1'
else:
for n in liste_1:
self.a += n
else:
if 'X' in liste_1:
liste_1.remove('X')
if liste_1==[]:
self.b='1'
else:
for n in liste_1:
self.b += n
else:
for n in liste_1:
self.c += n
new_liste = liste [i+1:]
signe = liste[i]
break
for j in range(len(new_liste)):
if new_liste[j]=='-' or new_liste[j]=='+':
liste_2 = new_liste[0:j]
if signe == '+':
pass
else:
liste_2=[signe]+liste_2
if '²' in liste_2:
liste_2.remove('X')
liste_2.remove('²')
if liste_2 == ['-']:
self.a='-1'
elif liste_2 == []:
self.a='1'
else:
for n in liste_2:
self.a += n
else:
if 'X' in liste_2:
liste_2.remove('X')
if liste_2 == ['-']:
self.b='-1'
elif liste_2 == []:
self.b='1'
else:
for n in liste_2:
self.b += n
else:
for n in liste_2:
self.c += n
new_liste_2 = new_liste [j+1:]
signe = new_liste[j]
break
for j in range(len(new_liste_2)):
if signe == '+':
pass
else:
new_liste_2=[signe]+new_liste_2
if '²' in new_liste_2:
new_liste_2.remove('X')
new_liste_2.remove('²')
if new_liste_2 == ['-']:
self.a='-1'
elif new_liste_2 == []:
self.a='1'
else:
for n in new_liste_2:
self.a += n
else:
if 'X' in new_liste_2:
new_liste_2.remove('X')
if new_liste_2 == ['-']:
self.b='-1'
elif new_liste_2 == []:
self.b='1'
else:
for n in new_liste_2:
self.b += n
else:
for n in new_liste_2:
self.c += n
break
#si l'operation commence par le signe -
else:
signe = liste[0]
del liste[0]
for i in range(len(liste)):
if liste[i]=='-' or liste[i]=='+':
liste_1 =[signe] + liste[0:i]
if '²' in liste_1:
liste_1.remove('X')
liste_1.remove('²')
if liste_1 == []:
self.a='1'
else:
for n in liste_1:
self.a += n
else:
if 'X' in liste_1:
liste_1.remove('X')
if liste_1==[]:
self.b='1'
else:
for n in liste_1:
self.b += n
else:
for n in liste_1:
self.c += n
new_liste = liste [i+1:]
signe = liste[i]
break
for j in range(len(new_liste)):
if new_liste[j]=='-' or new_liste[j]=='+':
liste_2 = new_liste[0:j]
if signe == '+':
pass
else:
liste_2=[signe]+liste_2
if '²' in liste_2:
liste_2.remove('X')
liste_2.remove('²')
if liste_2 == ['-']:
self.a='-1'
elif liste_2 == []:
self.a='1'
else:
for n in liste_2:
self.a += n
else:
if 'X' in liste_2:
liste_2.remove('X')
if liste_2 == ['-']:
self.b='-1'
elif liste_2 == []:
self.b='1'
else:
for n in liste_2:
self.b += n
else:
for n in liste_2:
self.c += n
new_liste_2 = new_liste [j+1:]
signe = new_liste[j]
break
for j in range(len(new_liste_2)):
if signe == '+':
pass
else:
new_liste_2=[signe]+new_liste_2
if '²' in new_liste_2:
new_liste_2.remove('X')
new_liste_2.remove('²')
if new_liste_2 == ['-']:
self.a='-1'
elif new_liste_2 == []:
self.a='1'
else:
for n in new_liste_2:
self.a += n
else:
if 'X' in new_liste_2:
new_liste_2.remove('X')
if new_liste_2 == ['-']:
self.b='-1'
elif new_liste_2 == []:
self.b='1'
else:
for n in new_liste_2:
self.b += n
else:
for n in new_liste_2:
self.c += n
break
disc = eval(self.b+'*'+self.b+'-'+'4'+'*'+self.a+'*'+self.c)
return str(disc)
except:
return 'Entrer un polynomes à trois monomes de haut dégré 2'
#afficher le discriminant du polynome entré
def print_disc(self):
disc = self.discriminant(self.ids.text_id.text)
self.ids.text_id.text = disc
#factorisation du polinome
def factorisation(self):
disc =float(self.discriminant(self.ids.text_id.text))
racine_disc = str(sqrt(disc))
x0 = eval('('+'-1'+'*'+self.b+')'+'/'+'('+'2'+'*'+self.a+')')
x1 = eval('('+'-1'+'*'+self.b+'-1'+'*'+racine_disc+')'+'/'+'('+'2'+'*'+self.a+')')
x2 = eval('('+'-1'+'*'+self.b+racine_disc+')'+'/'+'('+'2'+'*'+self.a+')')
if float(disc)==0:
x0 = eval('('+'-1'+self.b+')'+'/'+'2'+'*'+self.a)
#self.ids.text_id.text = self.a+'('+'X'+str(-*x0)+')'
print (round(x0, 2), round(x1, 2), round(x2, 2))
class CalcApp(App):
def build(self):
return Calco()
if __name__ == '__main__':
CalcApp().run()
calco.kv:
<Calco>:
canvas:
Color:
rgb: (249, 247, 247)
Rectangle:
pos: self.pos
size: self.size
orientation: 'vertical'
padding: 5
spacing: 5
BoxLayout:
orientation:
size_hint_y: .1
TextInput:
id: text_id
BoxLayout:
spacing: 5
BoxLayout:
orientation: 'vertical'
spacing: 5
BoxLayout:
size_hint_y: .2
padding_top: 5
padding_bottom: 5
padding_right: 5
spacing: 5
Button:
text: 'X'
on_press: text_id.text = text_id.text + self.text
Button:
text: 'Y'
on_press: text_id.text = text_id.text + self.text
Button:
text: '²'
on_press: text_id.text = text_id.text + self.text
Button:
text: 'Δ'
on_press: root.print_disc()
Button:
text: 'Fac'
on_press: root.factorisation()
GridLayout:
cols: 3
padding_right: 5
spacing: 5
Button:
text: '9'
on_press: text_id.text = text_id.text + self.text
Button:
text: '8'
on_press: text_id.text = text_id.text + self.text
Button:
text: '7'
on_press: text_id.text = text_id.text + self.text
Button:
text: '6'
on_press: text_id.text = text_id.text + self.text
Button:
text: '5'
on_press: text_id.text = text_id.text + self.text
Button:
text: '4'
on_press: text_id.text = text_id.text + self.text
Button:
text: '3'
on_press: text_id.text = text_id.text + self.text
Button:
text: '2'
on_press: text_id.text = text_id.text + self.text
Button:
text: '1'
on_press: text_id.text = text_id.text + self.text
Button:
text: '.'
on_press: text_id.text = text_id.text + self.text
Button:
text: '0'
on_press: text_id.text = text_id.text + self.text
Button:
text: '='
on_press: root.result()
BoxLayout:
orientation: 'vertical'
size_hint_x: .25
spacing: 5
Button:
text: 'Clear'
on_press: text_id.text = ''
Button:
text: '+'
on_press: text_id.text = text_id.text + self.text
Button:
text: '-'
on_press: text_id.text = text_id.text + self.text
Button:
text: 'x'
on_press: text_id.text = text_id.text + self.text
Button:
text: '/'
on_press: text_id.text = text_id.text + self.text
我已经修改了 .spec 文件以考虑 .kv 文件,但我仍然遇到相同的错误
calculatrice.spec
from kivy_deps import sdl2, glew
# -*- mode: python ; coding: utf-8 -*-
a = Analysis(
['calculatrice.py'],
pathex=[],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
noarchive=False,
optimize=0,
)
pyz = PYZ(a.pure)
a.datas += [('calco.kv', 'D:\\MON PROJET\\my test kivy\\calco\calco.kv', 'DATA')]
exe = EXE(
pyz,
a.scripts,
[],
exclude_binaries=True,
name='calculatrice',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
coll = COLLECT(
exe, Tree('D:\\MON PROJET\\my test kivy\\calco'),
a.binaries,
a.datas,
*[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
strip=False,
upx=True,
upx_exclude=[],
name='calculatrice',
)
这是错误消息:
[INFO ] [Logger ] Record log in C:\Users\hp\.kivy\logs\kivy_24-07-24_4.txt
[INFO ] [Kivy ] v2.1.0
[INFO ] [Kivy ] Installed at "D:\MON PROJET\my test kivy\calco\dist\calculatrice\_internal\kivy\__init__.pyc"
[INFO ] [Python ] v3.10.10 (tags/v3.10.10:aad5f6a, Feb 7 2023, 17:20:36) [MSC v.1929 64 bit (AMD64)]
[INFO ] [Python ] Interpreter at "D:\MON PROJET\my test kivy\calco\dist\calculatrice\calculatrice.exe"
[INFO ] [Logger ] Purge log fired. Processing...
[INFO ] [Logger ] Purge finished!
[INFO ] [Factory ] 189 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
Traceback (most recent call last):
File "calculatrice.py", line 10, in <module>
Builder.load_file('calco.kv')
File "kivy\lang\builder.py", line 303, in load_file
FileNotFoundError: [Errno 2] No such file or directory: 'calco.kv'
[16728] Failed to execute script 'calculatrice' due to unhandled exception!
请帮我解决问题
根据我的经验,我总是将
pathex
文件中的 spec
设置为包含顶级 python 文件的文件夹的路径。然后,将 kv
文件添加到 datas
列表中,作为 ('calco.kv', '.')
。