1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/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()