Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Tkinter import *
- def Demarrage():
- global val, calc, racine
- racine = Tk()
- racine.title('Calculatrice simple')
- racine.geometry('247x330+469+199')
- calc = Calculatrice(racine)
- racine.mainloop()
- class Calculatrice():
- def __init__(self,racine):
- principale = Frame(racine)
- self.ValeurCourante = 0
- self.ValeurAncienne = 0
- self.FonctionCourante = ''
- self.AffichageCourant = StringVar()
- self.AffichageCourant.set('0')
- self.PartieDecimale = False
- self.CompteDecimales = 0
- self.DefinirWidgets(principale)
- self.PlacerWidgets(principale)
- def DefinirWidgets(self,principale):
- self.lblAffichage = Label(principale,anchor=E,relief = SUNKEN,bg="white",height=2,textvariable=self.AffichageCourant)
- self.btn1 = Button(principale, text = '1',width = 4,height=3)
- self.btn1.bind('<ButtonRelease-1>', lambda e: self.foncBoutonNumerique(1))
- self.btn2 = Button(principale, text = '2',width = 4,height=3)
- self.btn2.bind('<ButtonRelease-1>', lambda e: self.foncBoutonNumerique(2))
- self.btn3 = Button(principale, text = '3',width = 4,height=3)
- self.btn3.bind('<ButtonRelease-1>', lambda e: self.foncBoutonNumerique(3))
- self.btn4 = Button(principale, text = '4',width = 4,height=3)
- self.btn4.bind('<ButtonRelease-1>', lambda e: self.foncBoutonNumerique(4))
- self.btn5 = Button(principale, text = '5',width = 4,height=3)
- self.btn5.bind('<ButtonRelease-1>', lambda e: self.foncBoutonNumerique(5))
- self.btn6 = Button(principale, text = '6',width = 4,height=3)
- self.btn6.bind('<ButtonRelease-1>', lambda e: self.foncBoutonNumerique(6))
- self.btn7 = Button(principale, text = '7',width = 4,height=3)
- self.btn7.bind('<ButtonRelease-1>', lambda e: self.foncBoutonNumerique(7))
- self.btn8 = Button(principale, text = '8',width = 4,height=3)
- self.btn8.bind('<ButtonRelease-1>', lambda e: self.foncBoutonNumerique(8))
- self.btn9 = Button(principale, text = '9',width = 4,height=3)
- self.btn9.bind('<ButtonRelease-1>', lambda e: self.foncBoutonNumerique(9))
- self.btn0 = Button(principale, text = '0',width = 4,height=3)
- self.btn0.bind('<ButtonRelease-1>', lambda e: self.foncBoutonNumerique(0))
- self.btnDash = Button(principale, text = '-',width = 4,height=3)
- self.btnDash.bind('<ButtonRelease-1>', lambda e: self.foncBoutonFonction('SIGNE'))
- self.btnDot = Button(principale, text = '.',width = 4,height=3)
- self.btnDot.bind('<ButtonRelease-1>', lambda e: self.foncBoutonFonction('Dec'))
- self.btnPlus = Button(principale,text = '+', width = 4, height=3)
- self.btnPlus.bind('<ButtonRelease-1>', lambda e: self.foncBoutonFonction('Ajouter'))
- self.btnMinus = Button(principale,text = '-', width = 4, height=3)
- self.btnMinus.bind('<ButtonRelease-1>', lambda e: self.foncBoutonFonction('Soustraire'))
- self.btnStar = Button(principale,text = '*', width = 4, height=3)
- self.btnStar.bind('<ButtonRelease-1>', lambda e: self.foncBoutonFonction('Multiplier'))
- self.btnDiv = Button(principale,text = '/', width = 4, height=3)
- self.btnDiv.bind('<ButtonRelease-1>', lambda e: self.foncBoutonFonction('Diviser'))
- self.btnEqual = Button(principale, text = '=')
- self.btnEqual.bind('<ButtonRelease-1>', lambda e: self.foncBoutonFonction('Egal'))
- self.btnClear = Button(principale, text = 'EFFACER')
- self.btnClear.bind('<ButtonRelease-1>', lambda e: self.foncEffacer())
- def PlacerWidgets(self,principale):
- principale.grid(column=0,row=0)
- self.lblAffichage.grid(column=0,row=0,columnspan = 4,sticky=EW)
- self.btn1.grid(column = 0, row = 1)
- self.btn2.grid(column = 1, row = 1)
- self.btn3.grid(column = 2, row = 1)
- self.btn4.grid(column = 0, row = 2)
- self.btn5.grid(column = 1, row = 2)
- self.btn6.grid(column = 2, row = 2)
- self.btn7.grid(column = 0, row = 3)
- self.btn8.grid(column = 1, row = 3)
- self.btn9.grid(column = 2, row = 3)
- self.btn0.grid(column = 1, row = 4)
- self.btnDash.grid(column = 0, row = 4)
- self.btnDot.grid(column = 2, row = 4)
- self.btnPlus.grid(column = 3,row = 1)
- self.btnMinus.grid(column = 3, row = 2)
- self.btnStar.grid(column = 3, row = 3)
- self.btnDiv.grid(column=3, row = 4)
- self.btnEqual.grid(column=0,row=5,columnspan = 4,sticky=NSEW)
- self.btnClear.grid(column=0,row=6,columnspan = 4, sticky = NSEW)
- def foncBoutonNumerique(self,val):
- if self.PartieDecimale == True:
- self.CompteDecimales += 1
- self.ValeurCourante = self.ValeurCourante + (val * (10**-self.CompteDecimales))
- else:
- self.ValeurCourante = (self.ValeurCourante * 10) + val
- self.Rafraichir()
- def foncEffacer(self):
- self.ValeurCourante = 0
- self.ValeurAncienne = 0
- self.Rafraichir()
- def foncBoutonFonction(self,fonction):
- if fonction == 'Dec':
- self.PartieDecimale = True
- else:
- self.PartieDecimale = False
- self.CompteDecimales = 0
- if fonction == 'SIGNE':
- self.ValeurCourante *= -1
- self.Rafraichir()
- elif fonction == 'Ajouter':
- self.ValeurAncienne = self.ValeurCourante
- self.ValeurCourante = 0
- self.FonctionCourante = 'Ajouter'
- elif fonction == 'Soustraire':
- self.ValeurAncienne = self.ValeurCourante
- self.ValeurCourante = 0
- self.FonctionCourante = 'Soustraire'
- elif fonction == 'Multiplier':
- self.ValeurAncienne = self.ValeurCourante
- self.ValeurCourante = 0
- self.FonctionCourante = 'Multiplier'
- elif fonction == 'Diviser':
- self.ValeurAncienne = self.ValeurCourante
- self.ValeurCourante = 0
- self.FonctionCourante = 'Diviser'
- elif fonction == 'Egal':
- if self.FonctionCourante == 'Ajouter':
- self.ValeurCourante += self.ValeurAncienne
- elif self.FonctionCourante == 'Soustraire':
- self.ValeurCourante = self.ValeurAncienne - self.ValeurCourante
- elif self.FonctionCourante == 'Multiplier':
- self.ValeurCourante *= self.ValeurAncienne
- elif self.FonctionCourante == 'Diviser':
- self.ValeurCourante = self.ValeurAncienne / self.ValeurCourante
- self.Rafraichir()
- self.ValeurCourante = 0
- self.ValeurAncienne = 0
- def Rafraichir(self):
- print('ValeurCourante = {0} - ValeurAncienne = {1}'.format(self.ValeurCourante,self.ValeurAncienne))
- self.AffichageCourant.set(self.ValeurCourante)
- if __name__ == '__main__':
- Demarrage()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement