Advertisement
Guest User

Programmer en Python partie 25, Calc.py

a guest
Aug 16th, 2011
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.14 KB | None | 0 0
  1. from Tkinter import *
  2.  
  3. def Demarrage():
  4.     global val, calc, racine
  5.     racine = Tk()
  6.     racine.title('Calculatrice simple')
  7.     racine.geometry('247x330+469+199')
  8.     calc = Calculatrice(racine)
  9.     racine.mainloop()
  10.    
  11. class Calculatrice():
  12.     def __init__(self,racine):
  13.         principale = Frame(racine)
  14.         self.ValeurCourante = 0
  15.         self.ValeurAncienne = 0
  16.         self.FonctionCourante = ''
  17.         self.AffichageCourant = StringVar()
  18.         self.AffichageCourant.set('0')
  19.         self.PartieDecimale = False
  20.         self.CompteDecimales = 0
  21.         self.DefinirWidgets(principale)
  22.         self.PlacerWidgets(principale)
  23.        
  24.     def DefinirWidgets(self,principale):
  25.         self.lblAffichage = Label(principale,anchor=E,relief = SUNKEN,bg="white",height=2,textvariable=self.AffichageCourant)
  26.         self.btn1 = Button(principale, text = '1',width = 4,height=3)
  27.         self.btn1.bind('<ButtonRelease-1>', lambda e: self.foncBoutonNumerique(1))
  28.         self.btn2 = Button(principale, text = '2',width = 4,height=3)
  29.         self.btn2.bind('<ButtonRelease-1>', lambda e: self.foncBoutonNumerique(2))
  30.         self.btn3 = Button(principale, text = '3',width = 4,height=3)
  31.         self.btn3.bind('<ButtonRelease-1>', lambda e: self.foncBoutonNumerique(3))
  32.         self.btn4 = Button(principale, text = '4',width = 4,height=3)
  33.         self.btn4.bind('<ButtonRelease-1>', lambda e: self.foncBoutonNumerique(4))
  34.         self.btn5 = Button(principale, text = '5',width = 4,height=3)
  35.         self.btn5.bind('<ButtonRelease-1>', lambda e: self.foncBoutonNumerique(5))
  36.         self.btn6 = Button(principale, text = '6',width = 4,height=3)
  37.         self.btn6.bind('<ButtonRelease-1>', lambda e: self.foncBoutonNumerique(6))
  38.         self.btn7 = Button(principale, text = '7',width = 4,height=3)
  39.         self.btn7.bind('<ButtonRelease-1>', lambda e: self.foncBoutonNumerique(7))
  40.         self.btn8 = Button(principale, text = '8',width = 4,height=3)
  41.         self.btn8.bind('<ButtonRelease-1>', lambda e: self.foncBoutonNumerique(8))
  42.         self.btn9 = Button(principale, text = '9',width = 4,height=3)
  43.         self.btn9.bind('<ButtonRelease-1>', lambda e: self.foncBoutonNumerique(9))
  44.         self.btn0 = Button(principale, text = '0',width = 4,height=3)
  45.         self.btn0.bind('<ButtonRelease-1>', lambda e: self.foncBoutonNumerique(0))
  46.         self.btnDash = Button(principale, text = '-',width = 4,height=3)
  47.         self.btnDash.bind('<ButtonRelease-1>', lambda e: self.foncBoutonFonction('SIGNE'))
  48.         self.btnDot = Button(principale, text = '.',width = 4,height=3)
  49.         self.btnDot.bind('<ButtonRelease-1>', lambda e: self.foncBoutonFonction('Dec'))
  50.         self.btnPlus = Button(principale,text = '+', width = 4, height=3)
  51.         self.btnPlus.bind('<ButtonRelease-1>', lambda e: self.foncBoutonFonction('Ajouter'))
  52.         self.btnMinus = Button(principale,text = '-', width = 4, height=3)
  53.         self.btnMinus.bind('<ButtonRelease-1>', lambda e: self.foncBoutonFonction('Soustraire'))        
  54.         self.btnStar = Button(principale,text = '*', width = 4, height=3)
  55.         self.btnStar.bind('<ButtonRelease-1>', lambda e: self.foncBoutonFonction('Multiplier'))        
  56.         self.btnDiv = Button(principale,text = '/', width = 4, height=3)
  57.         self.btnDiv.bind('<ButtonRelease-1>', lambda e: self.foncBoutonFonction('Diviser'))        
  58.         self.btnEqual = Button(principale, text = '=')
  59.         self.btnEqual.bind('<ButtonRelease-1>', lambda e: self.foncBoutonFonction('Egal'))
  60.         self.btnClear = Button(principale, text = 'EFFACER')
  61.         self.btnClear.bind('<ButtonRelease-1>', lambda e: self.foncEffacer())
  62.        
  63.     def PlacerWidgets(self,principale):
  64.         principale.grid(column=0,row=0)
  65.         self.lblAffichage.grid(column=0,row=0,columnspan = 4,sticky=EW)
  66.         self.btn1.grid(column = 0, row = 1)
  67.         self.btn2.grid(column = 1, row = 1)
  68.         self.btn3.grid(column = 2, row = 1)
  69.         self.btn4.grid(column = 0, row = 2)
  70.         self.btn5.grid(column = 1, row = 2)
  71.         self.btn6.grid(column = 2, row = 2)
  72.         self.btn7.grid(column = 0, row = 3)
  73.         self.btn8.grid(column = 1, row = 3)
  74.         self.btn9.grid(column = 2, row = 3)
  75.         self.btn0.grid(column = 1, row = 4)
  76.         self.btnDash.grid(column = 0, row = 4)
  77.         self.btnDot.grid(column = 2, row = 4)
  78.         self.btnPlus.grid(column = 3,row = 1)
  79.         self.btnMinus.grid(column = 3, row = 2)
  80.         self.btnStar.grid(column = 3, row = 3)
  81.         self.btnDiv.grid(column=3, row = 4)
  82.         self.btnEqual.grid(column=0,row=5,columnspan = 4,sticky=NSEW)
  83.         self.btnClear.grid(column=0,row=6,columnspan = 4, sticky = NSEW)
  84.        
  85.     def foncBoutonNumerique(self,val):
  86.         if self.PartieDecimale == True:
  87.             self.CompteDecimales += 1
  88.             self.ValeurCourante = self.ValeurCourante + (val * (10**-self.CompteDecimales))
  89.         else:
  90.             self.ValeurCourante = (self.ValeurCourante * 10) + val        
  91.         self.Rafraichir()
  92.        
  93.     def foncEffacer(self):
  94.         self.ValeurCourante = 0
  95.         self.ValeurAncienne = 0
  96.         self.Rafraichir()
  97.        
  98.     def foncBoutonFonction(self,fonction):
  99.         if fonction == 'Dec':
  100.             self.PartieDecimale = True
  101.         else:
  102.             self.PartieDecimale = False
  103.             self.CompteDecimales = 0
  104.             if fonction == 'SIGNE':
  105.                 self.ValeurCourante *= -1
  106.                 self.Rafraichir()
  107.             elif fonction == 'Ajouter':
  108.                 self.ValeurAncienne = self.ValeurCourante
  109.                 self.ValeurCourante = 0
  110.                 self.FonctionCourante = 'Ajouter'
  111.             elif fonction == 'Soustraire':
  112.                 self.ValeurAncienne = self.ValeurCourante
  113.                 self.ValeurCourante = 0
  114.                 self.FonctionCourante = 'Soustraire'
  115.             elif fonction == 'Multiplier':
  116.                 self.ValeurAncienne = self.ValeurCourante
  117.                 self.ValeurCourante = 0
  118.                 self.FonctionCourante = 'Multiplier'
  119.             elif fonction == 'Diviser':
  120.                 self.ValeurAncienne = self.ValeurCourante
  121.                 self.ValeurCourante = 0
  122.                 self.FonctionCourante = 'Diviser'            
  123.             elif fonction == 'Egal':
  124.                 if self.FonctionCourante == 'Ajouter':
  125.                     self.ValeurCourante += self.ValeurAncienne
  126.                 elif self.FonctionCourante == 'Soustraire':
  127.                     self.ValeurCourante = self.ValeurAncienne - self.ValeurCourante
  128.                 elif self.FonctionCourante == 'Multiplier':
  129.                     self.ValeurCourante *= self.ValeurAncienne
  130.                 elif self.FonctionCourante == 'Diviser':
  131.                     self.ValeurCourante = self.ValeurAncienne / self.ValeurCourante
  132.                 self.Rafraichir()
  133.                 self.ValeurCourante = 0
  134.                 self.ValeurAncienne = 0
  135.                
  136.     def Rafraichir(self):
  137.         print('ValeurCourante = {0} - ValeurAncienne = {1}'.format(self.ValeurCourante,self.ValeurAncienne))
  138.         self.AffichageCourant.set(self.ValeurCourante)
  139.        
  140. if __name__ == '__main__':
  141.     Demarrage()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement