I have a .py file called calculatrice.py and another .kv file called calco.kv. I am trying to produce an .exe with pyinstaller. Despite the instructions I followed in the kivy ‘Programming Guide “ Create a package for Windows” documentation, the .exe product does not run because it cannot find the calco.kv file.
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
I’ve modified the .spec file to take the .kv file into account, but I still get the same error
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\calcocalco.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',
)
This is the error message:
[INFO ] [Logger ] Record log in C:Usershp.kivylogskivy_24-07-24_4.txt
[INFO ] [Kivy ] v2.1.0
[INFO ] [Kivy ] Installed at "D:MON PROJETmy test kivycalcodistcalculatrice_internalkivy__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 PROJETmy test kivycalcodistcalculatricecalculatrice.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 "kivylangbuilder.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!
Please help me to solve the problem