#!/usr/bin/env python
import Tkinter as T
import math as m
class Window:
def __init__(self):
"This init method contains the main window of the script"
self.root = T.Tk(className=' Taschenrechner')
label = T.Label(self.root,text='Choose a function:')
label.pack(side='top')
self.frame = T.Frame(self.root)
self.frame.pack(side='top',fill='both')
self.func = T.IntVar()
button = T.Radiobutton(self.frame,text='sin()',variable=self.func,value=0)
button.grid(row=0, column=0)
button = T.Radiobutton(self.frame,text='cos()',variable=self.func,value=1)
button.grid(row=1, column=0)
button = T.Radiobutton(self.frame,text='tan()',variable=self.func,value=2)
button.grid(row=2, column=0)
button = T.Radiobutton(self.frame,text='log()',variable=self.func,value=3)
button.grid(row=0, column=1)
button = T.Radiobutton(self.frame,text='exp()',variable=self.func,value=4)
button.grid(row=1, column=1)
button = T.Radiobutton(self.frame,text='x**2()',variable=self.func,value=5)
button.grid(row=2, column=1)
self.erg_frame = T.Frame(self.root,relief='groove')
self.erg_frame.pack(side='top',fill='both')
self.ergebnis = T.Label(self.erg_frame,text='')
self.ergebnis.pack(side='top',fill='both')
self.qframe = T.Frame(self.root)
self.qframe.pack(side='bottom',fill='x')
self.qbutton = T.Button(self.qframe,text='Quit',command=self.root.destroy)
self.qbutton.pack(side='right')
self.ebutton = T.Button(self.qframe,text='Calucate',command=self.calc)
self.ebutton.pack(side='right')
self.root.mainloop()
def calc(self):
"Determines which function has been chosen and prompts for a value"
func = self.func.get()
if func==0:
func_str = 'sin(x), x = ?'
func = m.sin
elif func==1:
func_str = 'cos(x), x = ?'
func = m.cos
elif func==2:
func_str = 'tan(x), x = ?'
func = m.tan
elif func==3:
func_str = 'log(x), x = ?'
func = m.log
elif func==4:
func_str = 'exp(x), x = ?'
func = m.exp
elif func==5:
func_str = 'x**2, x = ?'
func = lambda x: x*x
self.function = func
self.top = T.Toplevel(self.root)
label = T.Label(self.top,text=func_str)
label.pack()
self.field = T.Entry(self.top)
self.field.pack(fill='both')
self.field.bind('<Return>',self.do_calc)
def do_calc(self,event):
"Reads the value and return the result in the main window2"
value = self.field.get()
self.top.destroy()
del self.top
try:
value = float(value)
except:
self.ergebnis.configure(text='Input was no valid numeric value')
else:
self.ergebnis.configure(text=str(self.function(value)))
if __name__ == '__main__':
w = Window()