Bonjour, j'ai un problème avec Kivy. J'ai essayé de faire une calculatrice avancée pour m'amuser et
voilà mon code .py et mon code .kv et mon erreur :
le .py :
import kivy
kivy.require('2.0.0')
from kivy.app import App
from kivy.config import Config
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
class Calculatrice(BoxLayout):
def calculer(self, expression):
if expression:
try:
self.display.text = str(eval(expression))
except Exception:
self.display.text = 'Erreur'
def renitialiser(self, expression):
if expression:
self.display.text = ''
class calculatriceApp(App):
def build(self):
self.title = 'Calculatrice'
Config.set('graphics', 'width', '250')
Config.set('graphics', 'height', '415')
calculatriceApp().run()
le .kv :
#:import hex kivy.utils.get_color_from_hex
<BouttonGris@Button>:
font_size: 25
background_color: 0,0,0,0
canvas.before:
Color:
rgba: hex('#2e2b2b') if self.state == 'normal' else (0,.7,.7,1)
Ellipse:
pos: self.pos
size: 60, 60
<BouttonBlanc@Button>:
font_size: 25
background_color: 0,0,0,0
canvas.before:
Color:
rgba: hex('#ffffff') if self.state == 'normal' else (0,.7,.7,1)
Ellipse:
pos: self.pos
size: 60, 60
<BouttonJaune@Button>:
font_size: 25
background_color: 0,0,0,0
canvas.before:
Color:
rgba: hex('#ffa20e') if self.state == 'normal' else (0,.7,.7,1)
Ellipse:
pos: self.pos
size: 60, 60
<BouttonRectangle@Button>:
font_size: 25
background_color: 0,0,0,0
canvas.before:
Color:
rgba: hex('#2e2b2b') if self.state == 'normal' else (0,.7,.7,1)
RoundedRedtancle:
pos: self.pos
size: 120, 60
radius: [30,]
Calcutrice:
id:calculatrice
display: input
orientation: 'vertical'
GridLayout:
orientation: 'horizontal'
size_hint: (1, 0.3)
cols: 1
rows: 1
TextInput:
id: input
background_color: hex('#000000')
foreground_color: hex('#ffffff')
font_size: 30
GridLayout:
orientation: 'vertical'
cols: 4
BouttonBlanc:
text: 'C'
on_press: calculatrice.renitialiser(input.text)
BouttonBlanc:
text: '+/-'
on_press: input.text += self.text
BouttonBlanc:
text: '%'
on_press: input.text += self.text
BouttonJaune:
text: '/'
on_press: input.text += self.text
BouttonGris:
text: '7'
on_press: input.text += self.text
BouttonGris:
text: '8'
on_press: input.text += self.text
BouttonGris:
text: '9'
on_press: input.text += self.text
BouttonJaune:
text: '*'
on_press: input.text += self.text
BouttonGris:
text: '4'
on_press: input.text += self.text
BouttonGris:
text: '5'
on_press: input.text += self.text
BouttonGris:
text: '6'
on_press: input.text += self.text
BouttonJaune:
text: '-'
on_press: input.text += self.text
BouttonGris:
text: '1'
on_press: input.text += self.text
BouttonGris:
text: '2'
on_press: input.text += self.text
BouttonGris:
text: '3'
on_press: input.text += self.text
BouttonJaune:
text: '+'
on_press: input.text += self.text
BouttonRectangle:
text: '0'
on_press: input.text += self.text
BouttonGris:
text: ','
on_press: input.text += self.text
BouttonJaune:
text: '='
on_press: calculatrice.calculer(input.text)
l'erreur :
#:import hex kivy.utils.get_color_from_hex
<BouttonGris@Button>:
font_size: 25
background_color: 0,0,0,0
canvas.before:
Color:
rgba: hex('#2e2b2b') if self.state == 'normal' else (0,.7,.7,1)
Ellipse:
pos: self.pos
size: 60, 60
<BouttonBlanc@Button>:
font_size: 25
background_color: 0,0,0,0
canvas.before:
Color:
rgba: hex('#ffffff') if self.state == 'normal' else (0,.7,.7,1)
Ellipse:
pos: self.pos
size: 60, 60
<BouttonJaune@Button>:
font_size: 25
background_color: 0,0,0,0
canvas.before:
Color:
rgba: hex('#ffa20e') if self.state == 'normal' else (0,.7,.7,1)
Ellipse:
pos: self.pos
size: 60, 60
<BouttonRectangle@Button>:
font_size: 25
background_color: 0,0,0,0
canvas.before:
Color:
rgba: hex('#2e2b2b') if self.state == 'normal' else (0,.7,.7,1)
RoundedRedtancle:
pos: self.pos
size: 120, 60
radius: [30,]
Calcutrice:
id:calculatrice
display: input
orientation: 'vertical'
GridLayout:
orientation: 'horizontal'
size_hint: (1, 0.3)
cols: 1
rows: 1
TextInput:
id: input
background_color: hex('#000000')
foreground_color: hex('#ffffff')
font_size: 30
GridLayout:
orientation: 'vertical'
cols: 4
BouttonBlanc:
text: 'C'
on_press: calculatrice.renitialiser(input.text)
BouttonBlanc:
text: '+/-'
on_press: input.text += self.text
BouttonBlanc:
text: '%'
on_press: input.text += self.text
BouttonJaune:
text: '/'
on_press: input.text += self.text
BouttonGris:
text: '7'
on_press: input.text += self.text
BouttonGris:
text: '8'
on_press: input.text += self.text
BouttonGris:
text: '9'
on_press: input.text += self.text
BouttonJaune:
text: '*'
on_press: input.text += self.text
BouttonGris:
text: '4'
on_press: input.text += self.text
BouttonGris:
text: '5'
on_press: input.text += self.text
BouttonGris:
text: '6'
on_press: input.text += self.text
BouttonJaune:
text: '-'
on_press: input.text += self.text
BouttonGris:
text: '1'
on_press: input.text += self.text
BouttonGris:
text: '2'
on_press: input.text += self.text
BouttonGris:
text: '3'
on_press: input.text += self.text
BouttonJaune:
text: '+'
on_press: input.text += self.text
BouttonRectangle:
text: '0'
on_press: input.text += self.text
BouttonGris:
text: ','
on_press: input.text += self.text
BouttonJaune:
text: '='
on_press: calculatrice.calculer(input.text)
Voilà, toute réponse sera acceptée merci ! :)
Modifié le 21 juil. 2021 à 19:37