Support Forums

Full Version: The Python Calculator
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Here's a very simple Python calculator that supports all the math functions and arithmetic...

To use just execute and type the math/arithmetic into the entry field. When your finished hit the return key.

for example type into the entry field sin(67) * 56 / (45 + cos(12) - log(345))
and the hit the return key

To clear the entry field press the escape key...

This is a demo for all the Python newbies...Have fun, Python is a great language

Code:
#! /usr/bin/python
# -*- coding: iso-8859-1 -*-

import Tkinter
from math import *

class MyCal:
    def __init__(self, master):
        self.ans = 0
        self.frame = Tkinter.Frame(master)
        self.entry = Tkinter.Entry(self.frame, width = 60, bg = 'white',\
                fg = 'black')

        self.entry.bind('<Return>', self.calit)
        self.entry.bind('<Escape>', self.eraseit)
        

        self.entry.pack()
        self.frame.pack()

    def eraseit(self, event):
        self.entry.delete(0, Tkinter.END)

    def calit(self, event):
        try:
            self.ans = eval(str(self.entry.get()))
            self.entry.delete(0, Tkinter.END)
            self.entry.insert(0, str(self.ans))
        except:
            self.entry.delete(0, Tkinter.END)
            self.entry.insert(0, 'Illegal instruction')


root = Tkinter.Tk()
root.title('Calculator')

cal = MyCal(root)

root.mainloop()