Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The Python Calculator
#1
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()
Slackware 13/ArchLinux - C/Assem/Python
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Help Dεlluzion 3 2,006 09-30-2019, 12:59 AM
Last Post: samsmith001
  Simple Python Python Compiler Canoris 21 9,201 06-01-2011, 06:30 PM
Last Post: Filefinder
  Python 2 vs 3? Jake 8 2,667 12-11-2010, 04:13 PM
Last Post: Bursihido
  [Tutorial] Advanced Tkinter Calculator uber1337 8 5,641 04-06-2010, 11:51 AM
Last Post: uber1337
  Complete Tkinter Calculator uber1337 0 1,764 03-06-2010, 09:49 AM
Last Post: uber1337

Forum Jump:


Users browsing this thread: 1 Guest(s)