vegaseat

tkinter tiny calculator updated

Mar 13th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.24 KB | None | 0 0
  1. ''' tk_calculator_tiny2.py
  2. An updated tiny calculator using the Tkinter GUI toolkit
  3. (note: 025 would be an octal number)
  4. You can type in functions contained in module math
  5. for instance type in  tan(pi/180)  then click  =
  6. tested with Python27 and Python33  by  vegaseat  13nov2013
  7. '''
  8.  
  9. # avoid integer division by Python2
  10. from __future__ import division
  11. from math import *
  12. from functools import partial
  13. try:
  14.     # Python2
  15.     import Tkinter as tk
  16. except ImportError:
  17.     # Python3
  18.     import tkinter as tk
  19.  
  20. class MyApp(tk.Tk):
  21.     def __init__(self):
  22.         # the root will be self
  23.         tk.Tk.__init__(self)
  24.         self.title("Tiny TK Calculator")
  25.         # use width x height + x_offset + y_offset (no spaces!)
  26.         #self.geometry("300x150+150+50")
  27.         # or set x, y position only
  28.         self.geometry("+150+50")
  29.         self.memory = 0
  30.         self.create_widgets()
  31.  
  32.     def create_widgets(self):
  33.         # this also shows the calculator's button layout
  34.         btn_list = [
  35.         '7',  '8',  '9',  '*',  'C',
  36.         '4',  '5',  '6',  '/',  'M->',
  37.         '1',  '2',  '3',  '-',  '->M',
  38.         '0',  '.',  '=',  '+',  'neg' ]
  39.  
  40.         rel = 'ridge'
  41.         # create all buttons with a loop
  42.         r = 1
  43.         c = 0
  44.         for b in btn_list:
  45.             # partial takes care of function and argument
  46.             cmd = partial(self.calculate, b)
  47.             tk.Button(self, text=b, width=5, relief=rel,
  48.                 command=cmd).grid(row=r, column=c)
  49.             c += 1
  50.             if c > 4:
  51.                 c = 0
  52.                 r += 1
  53.         # use an Entry widget for an editable display
  54.         self.entry = tk.Entry(self, width=33, bg="yellow")
  55.         self.entry.grid(row=0, column=0, columnspan=5)
  56.  
  57.     def calculate(self, key):
  58.         if key == '=':
  59.             # guard against the bad guys abusing eval()
  60.             if '_' in self.entry.get():
  61.                 self.entry.insert(tk.END, " not accepted!")
  62.             # here comes the calculation part
  63.             try:
  64.                 result = eval(self.entry.get())
  65.                 self.entry.insert(tk.END, " = " + str(result))
  66.             except:
  67.                 self.entry.insert(tk.END, "--> Error!")
  68.         elif key == 'C':
  69.             self.entry.delete(0, tk.END)  # clear entry
  70.         elif key == '->M':
  71.             self.memory = self.entry.get()
  72.             # extract the result
  73.             if '=' in self.memory:
  74.                 ix = self.memory.find('=')
  75.                 self.memory = self.memory[ix+2:]
  76.             self.title('M=' + self.memory)
  77.         elif key == 'M->':
  78.             if self.memory:
  79.                self.entry.insert(tk.END, self.memory)
  80.         elif key == 'neg':
  81.             if '=' in self.entry.get():
  82.                 self.entry.delete(0, tk.END)
  83.             try:
  84.                 if self.entry.get()[0] == '-':
  85.                     self.entry.delete(0)
  86.                 else:
  87.                     self.entry.insert(0, '-')
  88.             except IndexError:
  89.                 pass
  90.         else:
  91.             # previous calculation has been done, clear entry
  92.             if '=' in self.entry.get():
  93.                 self.entry.delete(0, tk.END)
  94.             self.entry.insert(tk.END, key)
  95.  
  96.  
  97. app = MyApp()
  98. app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment