Advertisement
Guest User

guicalc.py

a guest
Mar 31st, 2013
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.87 KB | None | 0 0
  1. # guicalc.py - a Python calculator
  2. from Tkinter import *
  3.  
  4. # the main class
  5. class Calc():
  6.     def __init__(self):
  7.         self.total = 0
  8.         self.current = ""
  9.         self.new_num = True
  10.         self.op_pending = False
  11.         self.op = ""
  12.         self.eq_flag = False
  13.  
  14.     def num_press(self, num):
  15.         temp = text_box.get()
  16.         self.eq_flag = False
  17.         temp2 = str(num)      
  18.         if self.new_num == True:
  19.             self.current = temp2
  20.             self.new_num = False
  21.         else:
  22.             if temp2 == '.':
  23.                 if temp2 in temp:
  24.                     return
  25.             self.current = temp + temp2
  26.         text_box.delete(0, END)
  27.         text_box.insert(0, self.current)
  28.  
  29.     def calc_total(self):
  30.         if self.op_pending == True:
  31.             self.do_sum()
  32.             self.op_pending = False
  33.    
  34.     def do_sum(self):
  35.         self.current = float(self.current)
  36.         if self.op == "add":
  37.             self.total += self.current
  38.         if self.op == "minus":
  39.             self.total -= self.current
  40.         if self.op == "times":
  41.             self.total *= self.current
  42.         if self.op == "divide":
  43.             self.total /= self.current
  44.         text_box.delete(0, END)
  45.         text_box.insert(0, self.total)
  46.         self.new_num = True
  47.  
  48.     def operation(self, op):
  49.         if self.op_pending == True:
  50.             self.do_sum()
  51.             self.op = op
  52.         else:
  53.             self.op_pending = True
  54.             if self.eq_flag == False:
  55.                 self.total = float(text_box.get())
  56.             else:
  57.                 self.total = self.current
  58.             self.new_num = True
  59.             self.op = op
  60.             self.eq_flag = False
  61.        
  62.     def cancel(self):
  63.         text_box.delete(0, END)
  64.         text_box.insert(0, "0")
  65.         self.new_num = True
  66.  
  67.     def all_cancel(self):
  68.         self.cancel()
  69.         self.total = 0
  70.  
  71.     def sign(self):
  72.         self.current = -(float(text_box.get()))
  73.         text_box.delete(0, END)
  74.         text_box.insert(0, self.current)
  75.  
  76. class My_Btn(Button):
  77.     def btn_cmd(self, num):
  78.         self["command"] = lambda: sum1.num_press(num)
  79.  
  80. sum1 = Calc()
  81. root = Tk()
  82. calc = Frame(root)
  83. calc.grid()
  84.  
  85. root.title("Calculator")
  86. text_box = Entry(calc, justify=RIGHT)
  87. text_box.grid(row = 0, column = 0, columnspan = 3, pady = 5)
  88. text_box.insert(0, "0")
  89.  
  90. # make the buttons
  91. numbers = "789456123"
  92. i = 0
  93. bttn = []
  94. for j in range(1,4):
  95.     for k in range(3):
  96.         bttn.append(My_Btn(calc, text = numbers[i]))
  97.         bttn[i].grid(row = j, column = k, pady = 5)
  98.         bttn[i].btn_cmd(numbers[i])
  99.         i += 1
  100. bttn_0 = Button(calc, text = "0")
  101. bttn_0["command"] = lambda: sum1.num_press(0)
  102. bttn_0.grid(row = 4, column = 1, pady = 5)
  103.  
  104. bttn_div = Button(calc, text = chr(247))
  105. bttn_div["command"] = lambda: sum1.operation("divide")
  106. bttn_div.grid(row = 1, column = 3, pady = 5)
  107.  
  108. bttn_mult = Button(calc, text = "x")
  109. bttn_mult["command"] = lambda: sum1.operation("times")
  110. bttn_mult.grid(row = 2, column = 3, pady = 5)
  111.  
  112. minus = Button(calc, text = "-")
  113. minus["command"] = lambda: sum1.operation("minus")
  114. minus.grid(row = 3, column = 3, pady = 5)
  115.  
  116. point = Button(calc, text = ".")
  117. point["command"] = lambda: sum1.num_press(".")
  118. point.grid(row = 4, column = 0, pady = 5)
  119.  
  120. add = Button(calc, text = "+")
  121. add["command"] = lambda: sum1.operation("add")
  122. add.grid(row = 4, column = 3, pady = 5)
  123.  
  124. neg= Button(calc, text = "+/-")
  125. neg["command"] = sum1.sign
  126. neg.grid(row = 5, column = 0, pady = 5)
  127.  
  128. clear = Button(calc, text = "C")
  129. clear["command"] = sum1.cancel
  130. clear.grid(row = 5, column = 1, pady = 5)
  131.  
  132. all_clear = Button(calc, text = "AC")
  133. all_clear["command"] = sum1.all_cancel
  134. all_clear.grid(row = 5, column = 2, pady = 5)
  135.  
  136. equals = Button(calc, text = "=")
  137. equals["command"] = sum1.calc_total
  138. equals.grid(row = 5, column = 3, pady = 5)
  139.  
  140. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement