This is the updated version of the Tiny Tkinter Calculator. It has more power, since you can type in functions contained in the Python math module then press the equals key. You can then move the result into memory for other calculations. The code is written in OOP class style as an exercise, still simple and hopefully very readable. Again, you are welcome to improve the functionality, but don't turn it into a HUGE calculator.
Updated Tiny Tkinter Calculator (Python)
''' tk_calculator_tiny2.py
A updated tiny calculator using the Tkinter GUI toolkit
you can type in functions contained in module math
for instance type in tan(pi/180) then click =
tested with Python27 and Python33 by vegaseat 13nov2013
'''
# avoid integer division by Python2
from __future__ import division
from math import *
from functools import partial
try:
# Python2
import Tkinter as tk
except ImportError:
# Python3
import tkinter as tk
class MyApp(tk.Tk):
def __init__(self):
# the root will be self
tk.Tk.__init__(self)
self.title("Tiny TK Calculator")
# use width x height + x_offset + y_offset (no spaces!)
#self.geometry("300x150+150+50")
# or set x, y position only
self.geometry("+150+50")
self.memory = 0
self.create_widgets()
def create_widgets(self):
# this also shows the calculator's button layout
btn_list = [
'7', '8', '9', '*', 'C',
'4', '5', '6', '/', 'M->',
'1', '2', '3', '-', '->M',
'0', '.', '=', '+', 'neg' ]
rel = 'ridge'
# create all buttons with a loop
r = 1
c = 0
for b in btn_list:
# partial takes care of function and argument
cmd = partial(self.calculate, b)
tk.Button(self, text=b, width=5, relief=rel,
command=cmd).grid(row=r, column=c)
c += 1
if c > 4:
c = 0
r += 1
# use an Entry widget for an editable display
self.entry = tk.Entry(self, width=33, bg="yellow")
self.entry.grid(row=0, column=0, columnspan=5)
def calculate(self, key):
if key == '=':
# guard against the bad guys abusing eval()
if '_' in self.entry.get():
self.entry.insert(tk.END, " not accepted!")
# here comes the calculation part
try:
result = eval(self.entry.get())
self.entry.insert(tk.END, " = " + str(result))
except:
self.entry.insert(tk.END, "--> Error!")
elif key == 'C':
self.entry.delete(0, tk.END) # clear entry
elif key == '->M':
self.memory = self.entry.get()
# extract the result
if '=' in self.memory:
ix = self.memory.find('=')
self.memory = self.memory[ix+2:]
self.title('M=' + self.memory)
elif key == 'M->':
if self.memory:
self.entry.insert(tk.END, self.memory)
elif key == 'neg':
if '=' in self.entry.get():
self.entry.delete(0, tk.END)
try:
if self.entry.get()[0] == '-':
self.entry.delete(0)
else:
self.entry.insert(0, '-')
except IndexError:
pass
else:
# previous calculation has been done, clear entry
if '=' in self.entry.get():
self.entry.delete(0, tk.END)
self.entry.insert(tk.END, key)
app = MyApp()
app.mainloop()
Assembly Guy 72 Posting Whiz
sneekula 969 Nearly a Posting Maven
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
BustACode 15 Light Poster
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
DragonMastur 23 Light Poster
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.