1
+ # Interfas Grafica IX
2
+
3
+ from tkinter import *
4
+
5
+ raiz = Tk ()
6
+
7
+ miFrame = Frame (raiz )
8
+
9
+ miFrame .pack ()
10
+
11
+ operacion = ""
12
+
13
+ reset_pantalla = False
14
+
15
+ resultado = 0
16
+
17
+
18
+ #-------------------------pantalla-------------------------------
19
+
20
+ numeroPantalla = StringVar ()
21
+
22
+ pantalla = Entry (miFrame , textvariable = numeroPantalla )
23
+ pantalla .grid (row = 1 , column = 1 , padx = 10 , pady = 10 , columnspan = 4 ) #Espaciador
24
+ pantalla .config (background = "black" ,fg = "#2EF907" , justify = "right" , width = 30 )
25
+
26
+
27
+ #-------------------pulsaciones teclado--------------------------
28
+
29
+ def numeroPulsado (num ):
30
+
31
+ global operacion
32
+
33
+ global reset_pantalla
34
+
35
+ if reset_pantalla != False :
36
+
37
+ numeroPantalla .set (num )
38
+
39
+ reset_pantalla = False
40
+
41
+ else :
42
+
43
+ numeroPantalla .set (numeroPantalla .get () + num )
44
+
45
+
46
+ #---------------------funcion suma----------------------------------
47
+
48
+ def suma (num ):
49
+
50
+ global operacion
51
+
52
+ global resultado
53
+
54
+ global reset_pantalla
55
+
56
+ resultado += int (num ) #resultado=resultado+int(num)
57
+
58
+ operacion = "suma"
59
+
60
+ reset_pantalla = True
61
+
62
+ numeroPantalla .set (resultado )
63
+
64
+
65
+
66
+ #--------------------------funcion resta------------------------------
67
+ num1 = 0
68
+
69
+ contador_resta = 0
70
+
71
+ def resta (num ):
72
+
73
+ global operacion
74
+
75
+ global resultado
76
+
77
+ global num1
78
+
79
+ global contador_resta
80
+
81
+ global reset_pantalla
82
+
83
+ if contador_resta == 0 :
84
+
85
+ num1 = int (num )
86
+
87
+ resultado = num1
88
+
89
+ else :
90
+
91
+ if contador_resta == 1 :
92
+
93
+ resultado = num1 - int (num )
94
+
95
+ else :
96
+
97
+ resultado = int (resultado )- int (num )
98
+
99
+ numeroPantalla .set (resultado )
100
+
101
+ resultado = numeroPantalla .get ()
102
+
103
+
104
+ contador_resta = contador_resta + 1
105
+
106
+ operacion = "resta"
107
+
108
+ reset_pantalla = True
109
+
110
+
111
+ #-----------------------funcion multiplicacion---------------------
112
+ contador_multi = 0
113
+
114
+ def multiplica (num ):
115
+
116
+ global operacion
117
+
118
+ global resultado
119
+
120
+ global num1
121
+
122
+ global contador_multi
123
+
124
+ global reset_pantalla
125
+
126
+ if contador_multi == 0 :
127
+
128
+ num1 = int (num )
129
+
130
+ resultado = num1
131
+
132
+ else :
133
+
134
+ if contador_multi == 1 :
135
+
136
+ resultado = num1 * int (num )
137
+
138
+ else :
139
+
140
+ resultado = int (resultado )* int (num )
141
+
142
+ numeroPantalla .set (resultado )
143
+
144
+ resultado = numeroPantalla .get ()
145
+
146
+
147
+ contador_multi = contador_multi + 1
148
+
149
+ operacion = "multiplicacion"
150
+
151
+ reset_pantalla = True
152
+
153
+ #--------------------------funcion division---------------------
154
+
155
+ contador_divi = 0
156
+
157
+ def divide (num ):
158
+
159
+ global operacion
160
+
161
+ global resultado
162
+
163
+ global num1
164
+
165
+ global contador_divi
166
+
167
+ global reset_pantalla
168
+
169
+ if contador_divi == 0 :
170
+
171
+ num1 = float (num )
172
+
173
+ resultado = num1
174
+
175
+ else :
176
+
177
+ if contador_divi == 1 :
178
+
179
+ resultado = num1 / float (num )
180
+
181
+ else :
182
+
183
+ resultado = float (resultado )/ float (num )
184
+
185
+ numeroPantalla .set (resultado )
186
+
187
+ resultado = numeroPantalla .get ()
188
+
189
+
190
+ contador_divi = contador_divi + 1
191
+
192
+ operacion = "division"
193
+
194
+ reset_pantalla = True
195
+
196
+
197
+
198
+ #-------------------------funcion el_resultado----------------
199
+
200
+ def el_resultado ():
201
+
202
+ global resultado
203
+
204
+ global operacion
205
+
206
+ global contador_resta
207
+
208
+ global contador_multi
209
+
210
+ global contador_divi
211
+
212
+
213
+ if operacion == "suma" :
214
+
215
+ numeroPantalla .set (resultado + int (numeroPantalla .get ()))
216
+
217
+ resultado = 0
218
+
219
+ elif operacion == "resta" :
220
+
221
+ numeroPantalla .set (int (resultado )- int (numeroPantalla .get ()))
222
+
223
+ resultado = 0
224
+
225
+ contador_resta = 0
226
+
227
+ elif operacion == "multiplicacion" :
228
+
229
+ numeroPantalla .set (int (resultado )* int (numeroPantalla .get ()))
230
+
231
+ resultado = 0
232
+
233
+ contador_multi = 0
234
+
235
+ elif operacion == "division" :
236
+
237
+ numeroPantalla .set (int (resultado )/ int (numeroPantalla .get ()))
238
+
239
+ resultado = 0
240
+
241
+ contador_divi = 0
242
+
243
+
244
+
245
+
246
+
247
+
248
+ #-------------fila 1---------------------------------------------
249
+
250
+ boton7 = Button (miFrame , text = "7" , width = 7 , height = 3 , command = lambda :numeroPulsado ("7" ))
251
+ boton7 .grid (row = 2 , column = 1 )
252
+ boton8 = Button (miFrame , text = "8" , width = 7 , height = 3 , command = lambda :numeroPulsado ("8" ))
253
+ boton8 .grid (row = 2 , column = 2 )
254
+ boton9 = Button (miFrame , text = "9" , width = 7 , height = 3 , command = lambda :numeroPulsado ("9" ))
255
+ boton9 .grid (row = 2 , column = 3 )
256
+ botonDiv = Button (miFrame , text = "/" , width = 7 , height = 3 , command = lambda :divide (numeroPantalla .get ()))
257
+ botonDiv .grid (row = 2 , column = 4 )
258
+
259
+
260
+ #-------------fila 2---------------------------------------------
261
+
262
+ boton4 = Button (miFrame , text = "4" , width = 7 , height = 3 , command = lambda :numeroPulsado ("4" ))
263
+ boton4 .grid (row = 3 , column = 1 )
264
+ boton5 = Button (miFrame , text = "5" , width = 7 , height = 3 , command = lambda :numeroPulsado ("5" ))
265
+ boton5 .grid (row = 3 , column = 2 )
266
+ boton6 = Button (miFrame , text = "6" , width = 7 , height = 3 , command = lambda :numeroPulsado ("6" ))
267
+ boton6 .grid (row = 3 , column = 3 )
268
+ botonMult = Button (miFrame , text = "*" , width = 7 , height = 3 , command = lambda :multiplica (numeroPantalla .get ()))
269
+ botonMult .grid (row = 3 , column = 4 )
270
+
271
+ #-------------fila 3---------------------------------------------
272
+
273
+ boton1 = Button (miFrame , text = "1" , width = 7 , height = 3 , command = lambda :numeroPulsado ("1" ))
274
+ boton1 .grid (row = 4 , column = 1 )
275
+ boton2 = Button (miFrame , text = "2" , width = 7 , height = 3 , command = lambda :numeroPulsado ("2" ))
276
+ boton2 .grid (row = 4 , column = 2 )
277
+ boton3 = Button (miFrame , text = "3" , width = 7 , height = 3 , command = lambda :numeroPulsado ("3" ))
278
+ boton3 .grid (row = 4 , column = 3 )
279
+ botonRest = Button (miFrame , text = "-" , width = 7 , height = 3 , command = lambda :resta (numeroPantalla .get ()))
280
+ botonRest .grid (row = 4 , column = 4 )
281
+
282
+
283
+ #-------------fila 4---------------------------------------------
284
+
285
+ boton0 = Button (miFrame , text = "0" , width = 7 , height = 3 , command = lambda :numeroPulsado ("0" ))
286
+ boton0 .grid (row = 5 , column = 1 )
287
+ botonComa = Button (miFrame , text = "," , width = 7 , height = 3 , command = lambda :numeroPulsado ("." ))
288
+ botonComa .grid (row = 5 , column = 2 )
289
+ botonIgual = Button (miFrame , text = "=" , width = 7 , height = 3 , command = lambda :el_resultado ())
290
+ botonIgual .grid (row = 5 , column = 3 )
291
+ botonSum = Button (miFrame , text = "+" , width = 7 , height = 3 , command = lambda :suma (numeroPantalla .get ()))
292
+ botonSum .grid (row = 5 , column = 4 )
293
+
294
+ raiz .mainloop ()
0 commit comments