Advertisement
gregwa

Beginning Python #25 Calc.py

Jul 7th, 2011
532
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from Tkinter import *
  2.  
  3. def StartUp():
  4.     global val, w, root
  5.     root = Tk()
  6.     root.title('Easy Calc')
  7.     root.geometry('247x330+469+199')
  8.     w = Calculator(root)
  9.     root.mainloop()
  10.    
  11. class Calculator():
  12.     def __init__(self,root):
  13.         master = Frame(root)
  14.         self.CurrentValue = 0
  15.         self.HolderValue = 0
  16.         self.CurrentFunction = ''
  17.         self.CurrentDisplay = StringVar()
  18.         self.CurrentDisplay.set('0')
  19.         self.DecimalNext = False
  20.         self.DecimalCount = 0
  21.         self.DefineWidgets(master)
  22.         self.PlaceWidgets(master)
  23.        
  24.     def DefineWidgets(self,master):
  25.         self.lblDisplay = Label(master,anchor=E,relief = SUNKEN,bg="white",height=2,textvariable=self.CurrentDisplay)
  26.         self.btn1 = Button(master, text = '1',width = 4,height=3)
  27.         self.btn1.bind('<ButtonRelease-1>', lambda e: self.funcNumButton(1))
  28.         self.btn2 = Button(master, text = '2',width = 4,height=3)
  29.         self.btn2.bind('<ButtonRelease-1>', lambda e: self.funcNumButton(2))
  30.         self.btn3 = Button(master, text = '3',width = 4,height=3)
  31.         self.btn3.bind('<ButtonRelease-1>', lambda e: self.funcNumButton(3))
  32.         self.btn4 = Button(master, text = '4',width = 4,height=3)
  33.         self.btn4.bind('<ButtonRelease-1>', lambda e: self.funcNumButton(4))
  34.         self.btn5 = Button(master, text = '5',width = 4,height=3)
  35.         self.btn5.bind('<ButtonRelease-1>', lambda e: self.funcNumButton(5))
  36.         self.btn6 = Button(master, text = '6',width = 4,height=3)
  37.         self.btn6.bind('<ButtonRelease-1>', lambda e: self.funcNumButton(6))
  38.         self.btn7 = Button(master, text = '7',width = 4,height=3)
  39.         self.btn7.bind('<ButtonRelease-1>', lambda e: self.funcNumButton(7))
  40.         self.btn8 = Button(master, text = '8',width = 4,height=3)
  41.         self.btn8.bind('<ButtonRelease-1>', lambda e: self.funcNumButton(8))
  42.         self.btn9 = Button(master, text = '9',width = 4,height=3)
  43.         self.btn9.bind('<ButtonRelease-1>', lambda e: self.funcNumButton(9))
  44.         self.btn0 = Button(master, text = '0',width = 4,height=3)
  45.         self.btn0.bind('<ButtonRelease-1>', lambda e: self.funcNumButton(0))
  46.         self.btnDash = Button(master, text = '-',width = 4,height=3)
  47.         self.btnDash.bind('<ButtonRelease-1>', lambda e: self.funcFuncButton('ABS'))
  48.         self.btnDot = Button(master, text = '.',width = 4,height=3)
  49.         self.btnDot.bind('<ButtonRelease-1>', lambda e: self.funcFuncButton('Dec'))
  50.         self.btnPlus = Button(master,text = '+', width = 4, height=3)
  51.         self.btnPlus.bind('<ButtonRelease-1>', lambda e: self.funcFuncButton('Add'))
  52.         self.btnMinus = Button(master,text = '-', width = 4, height=3)
  53.         self.btnMinus.bind('<ButtonRelease-1>', lambda e: self.funcFuncButton('Subtract'))        
  54.         self.btnStar = Button(master,text = '*', width = 4, height=3)
  55.         self.btnStar.bind('<ButtonRelease-1>', lambda e: self.funcFuncButton('Multiply'))        
  56.         self.btnDiv = Button(master,text = '/', width = 4, height=3)
  57.         self.btnDiv.bind('<ButtonRelease-1>', lambda e: self.funcFuncButton('Divide'))        
  58.         self.btnEqual = Button(master, text = '=')
  59.         self.btnEqual.bind('<ButtonRelease-1>', lambda e: self.funcFuncButton('Eq'))
  60.         self.btnClear = Button(master, text = 'CLEAR')
  61.         self.btnClear.bind('<ButtonRelease-1>', lambda e: self.funcClear())
  62.        
  63.     def PlaceWidgets(self,master):
  64.         master.grid(column=0,row=0)
  65.         self.lblDisplay.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 funcNumButton(self,val):
  86.         if self.DecimalNext == True:
  87.             self.DecimalCount += 1
  88.             self.CurrentValue = self.CurrentValue + (val * (10**-self.DecimalCount))
  89.         else:
  90.             self.CurrentValue = (self.CurrentValue * 10) + val        
  91.         self.DisplayIt()
  92.        
  93.     def funcClear(self):
  94.         self.CurrentValue = 0
  95.         self.HolderValue = 0
  96.         self.DisplayIt()
  97.        
  98.     def funcFuncButton(self,function):
  99.         if function == 'Dec':
  100.             self.DecimalNext = True
  101.         else:
  102.             self.DecimalNext = False
  103.             self.DecimalCount = 0
  104.             if function == 'ABS':
  105.                 self.CurrentValue *= -1
  106.                 self.DisplayIt()
  107.             elif function == 'Add':
  108.                 self.HolderValue = self.CurrentValue
  109.                 self.CurrentValue = 0
  110.                 self.CurrentFunction = 'Add'
  111.             elif function == 'Subtract':
  112.                 self.HolderValue = self.CurrentValue
  113.                 self.CurrentValue = 0
  114.                 self.CurrentFunction = 'Subtract'
  115.             elif function == 'Multiply':
  116.                 self.HolderValue = self.CurrentValue
  117.                 self.CurrentValue = 0
  118.                 self.CurrentFunction = 'Multiply'
  119.             elif function == 'Divide':
  120.                 self.HolderValue = self.CurrentValue
  121.                 self.CurrentValue = 0
  122.                 self.CurrentFunction = 'Divide'            
  123.             elif function == 'Eq':
  124.                 if self.CurrentFunction == 'Add':
  125.                     self.CurrentValue += self.HolderValue
  126.                 elif self.CurrentFunction == 'Subtract':
  127.                     self.CurrentValue = self.HolderValue - self.CurrentValue
  128.                 elif self.CurrentFunction == 'Multiply':
  129.                     self.CurrentValue *= self.HolderValue
  130.                 elif self.CurrentFunction == 'Divide':
  131.                     self.CurrentValue = self.HolderValue / self.CurrentValue
  132.                 self.DisplayIt()
  133.                 self.CurrentValue = 0
  134.                 self.HolderValue = 0
  135.                
  136.     def DisplayIt(self):
  137.         print('CurrentValue = {0} - HolderValue = {1}'.format(self.CurrentValue,self.HolderValue))
  138.         self.CurrentDisplay.set(self.CurrentValue)
  139.        
  140. if __name__ == '__main__':
  141.     StartUp()
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement