1
2
3 """
4 Calculadora simples com GUI em Tkinter.
5
6 """
7
8 __license__ = 'GPL, GNU Public License'
9 __author__ = 'Leonardo Gregianin'
10 __credits__ = 'http://doc.async.com.br/python/Tkinter/index.htm'
11
12 from Tkinter import *
13 from math import *
14 import sys
15
16 class Calculadora:
17 def __init__(self, toplevel):
18
19
20 toplevel.title('Calculadora')
21 toplevel.geometry("210x200")
22
23
24 self.frame1 = Frame(toplevel)
25 self.frame1.pack()
26
27
28 self.frame2 = Frame(toplevel)
29 self.frame2.pack()
30
31
32 self.frame3 = Frame(toplevel)
33 self.frame3.pack()
34
35
36 self.frame4 = Frame(toplevel, pady=12)
37 self.frame4.pack()
38
39
40 self.frame5 = Frame(toplevel)
41 self.frame5.pack()
42
43
44 self.frame6 = Frame(toplevel, pady=12)
45 self.frame6.pack()
46
47
48 Label(self.frame1,text='', fg='black',
49 font=('Verdana','10'), height=1).pack()
50 fonte1=('Verdana','10')
51
52
53 somar=Button(self.frame4,font=fonte1, text='+',command=self.somar)
54 somar.pack(side=LEFT)
55
56
57 subtrair=Button(self.frame4,font=fonte1, text='-',command=self.subtrair)
58 subtrair.pack(side=LEFT)
59
60
61 multiplicar=Button(self.frame4,font=fonte1, text='*',command=self.multiplicar)
62 multiplicar.pack(side=LEFT)
63
64
65 dividir=Button(self.frame4,font=fonte1, text='/',command=self.dividir)
66 dividir.pack(side=LEFT)
67
68
69 raiz=Button(self.frame4,font=fonte1, text='Sqrt',command=self.sqrt)
70 raiz.pack(side=LEFT)
71
72
73 limpar=Button(self.frame6, font=fonte1, text= 'Limpar', command=self.limpar)
74 limpar.pack(side=LEFT)
75
76
77 sair=Button(self.frame6, font=fonte1, text= 'Sair', command=self.sair)
78 sair.pack(side=LEFT)
79
80
81 Label(self.frame2,text='Valor1 :', font=fonte1,width=8).pack(side=LEFT)
82 self.valor1=Entry(self.frame2,width=10,font=fonte1)
83 self.valor1.focus_force()
84 self.valor1.pack(side=LEFT)
85
86
87 Label(self.frame3,text='Valor2 :',font=fonte1,width=8).pack(side=LEFT)
88 self.valor2=Entry(self.frame3,width=10,font=fonte1)
89 self.valor2.pack(side=LEFT)
90
91
92 Label(self.frame5,text=' = ',font=fonte1,width=8).pack(side=LEFT)
93 self.msg=Label(self.frame5,width=10,font=fonte1)
94 self.msg.pack(side=LEFT)
95
96 def somar(self):
97 valor1 = self.valor1.get()
98 valor2 = self.valor2.get()
99 valor1 = float(valor1)
100 valor2 = float(valor2)
101 c = valor1 + valor2
102 c = float(c)
103 self.msg['text']= '%f' %(c)
104
105 def subtrair(self):
106 valor1 = self.valor1.get()
107 valor2 = self.valor2.get()
108 valor1 = float(valor1)
109 valor2 = float(valor2)
110 c = valor1 - valor2
111 c = float(c)
112 self.msg['text']= '%f' %(c)
113
114 def multiplicar(self):
115 valor1 = self.valor1.get()
116 valor2 = self.valor2.get()
117 valor1 = float(valor1)
118 valor2 = float(valor2)
119 c = valor1 * valor2
120 c = float(c)
121 self.msg['text']= '%f' %(c)
122
123 def dividir(self):
124 valor1 = self.valor1.get()
125 valor2 = self.valor2.get()
126 valor1 = float(valor1)
127 valor2 = float(valor2)
128 c = valor1 / valor2
129 c = float(c)
130 self.msg['text']= '%f' %(c)
131
132 def sqrt(self):
133 try:
134 valor1 = self.valor1.get()
135 valor1 = float(valor1)
136 c = sqrt(valor1)
137 c = float(c)
138 self.msg['text']= '%f' %(c)
139 except:
140 valor2 = self.valor2.get()
141 valor2 = float(valor2)
142 c = sqrt(valor2)
143 c = float(c)
144 self.msg['text']= '%f' %(c)
145
146 def limpar(self):
147 pass
148
149 def sair(self):
150 app.destroy()
151
152 app=Tk()
153 Calculadora(app)
154 app.mainloop()