22
33class Calculadora :
44 def __init__ (self ):
5+ self .ponto = True
6+ self .error = False
57 self .root = Tk ()
68 self .config_window ()
9+ self .create_screen ()
710 self .create_buttons ()
811 self .root .mainloop ()
912
1013 def config_window (self ):
11- self .root .title ("Calculator " )
14+ self .root .title ("Calculadora " )
1215 self .root .geometry ("400x500" )
1316
1417 self .screen_frame = Frame (self .root , bg = '#2c2c2c' )
15- self .screen_frame .place (relx = 0 , rely = 0 , relwidth = 1 , relheight = 0.22 )
18+ self .screen_frame .place (relx = 0 , rely = 0 , relwidth = 1 , relheight = 0.23 )
1619
17- #The label that shows the results
20+ self .button_frame = Frame (self .root )
21+ self .button_frame .place (relx = 0.001 , rely = 0.23 , relwidth = 0.999 , relheight = 0.77 )
22+
23+ def create_screen (self ):
24+ #The label that shows the results
1825 self .equation = ''
1926 self .eqvar = StringVar ()
2027 self .eqvar .set (self .equation )
2128
22- self .resultLabel = Label (self .screen_frame , textvariable = self .eqvar ,
29+ self .result_frame = Frame (self .screen_frame , bg = '#2c2c2c' )
30+ self .result_frame .place (relx = 0 , rely = 0.5 , relwidth = 1 , relheight = 0.5 )
31+
32+ self .resultLabel = Label (self .result_frame , textvariable = self .eqvar ,
2333 bg = '#2c2c2c' , fg = '#ffffff' , font = (None , 18 ), anchor = "e" )
24- self .resultLabel .pack (side = RIGHT , padx = 20 , pady = (30 , 0 ))
34+ self .resultLabel .pack (side = RIGHT , padx = 20 , pady = (20 , 10 ))
35+
36+
37+ #Lastequation
38+ self .lastvar = StringVar ()
39+ self .lastvar .set ('' )
40+
41+ self .lasteq_frame = Frame (self .screen_frame , bg = '#2c2c2c' )
42+ self .lasteq_frame .place (relx = 0 , rely = 0 , relwidth = 1 , relheight = 0.5 )
43+
44+ self .lasteqLabel = Label (self .lasteq_frame , textvariable = self .lastvar ,
45+ bg = '#2c2c2c' , fg = '#777777' , font = (None , 18 ), anchor = "e" )
46+ self .lasteqLabel .pack (side = RIGHT , padx = 20 , pady = (30 , 0 ))
2547
26- self .button_frame = Frame (self .root )
27- self .button_frame .place (relx = 0.001 , rely = 0.23 , relwidth = 0.999 , relheight = 0.77 )
2848
2949 def create_buttons (self ):
3050 # Row 1
3151 self .ac = Button (self .button_frame , text = 'C' , command = self .clean )
32- self .ac .place (relx = 0 , rely = 0 , relwidth = 0.75 , relheight = 0.2 )
52+ self .ac .place (relx = 0 , rely = 0 , relwidth = 0.5 , relheight = 0.2 )
3353
34- self .bdivision = Button (self .button_frame , text = '/' , command = lambda : self .put_symbol ('/' ))
54+ self .berase = Button (self .button_frame , text = '⌫' , command = self .erase )
55+ self .berase .place (relx = 0.5 , rely = 0 , relwidth = 0.25 , relheight = 0.2 )
56+
57+ self .bdivision = Button (self .button_frame , text = '/' , command = lambda : self .operator (' / ' ))
3558 self .bdivision .place (relx = 0.75 , rely = 0 , relwidth = 0.25 , relheight = 0.2 )
3659
3760 # Row 2
@@ -44,7 +67,7 @@ def create_buttons(self):
4467 self .b9 = Button (self .button_frame , text = '9' , command = lambda : self .put_symbol ('9' ))
4568 self .b9 .place (relx = 0.5 , rely = 0.2 , relwidth = 0.25 , relheight = 0.2 )
4669
47- self .bmulti = Button (self .button_frame , text = '*' , command = lambda : self .put_symbol ( '* ' ))
70+ self .bmulti = Button (self .button_frame , text = '*' , command = lambda : self .operator ( ' * ' ))
4871 self .bmulti .place (relx = 0.75 , rely = 0.2 , relwidth = 0.25 , relheight = 0.2 )
4972
5073 # Row 3
@@ -57,7 +80,7 @@ def create_buttons(self):
5780 self .b6 = Button (self .button_frame , text = '6' , command = lambda : self .put_symbol ('6' ))
5881 self .b6 .place (relx = 0.5 , rely = 0.4 , relwidth = 0.25 , relheight = 0.2 )
5982
60- self .bsub = Button (self .button_frame , text = '-' , command = lambda : self .put_symbol ( '- ' ))
83+ self .bsub = Button (self .button_frame , text = '-' , command = lambda : self .operator ( ' - ' ))
6184 self .bsub .place (relx = 0.75 , rely = 0.4 , relwidth = 0.25 , relheight = 0.2 )
6285
6386 #Row 4
@@ -70,7 +93,7 @@ def create_buttons(self):
7093 self .b3 = Button (self .button_frame , text = '3' , command = lambda : self .put_symbol ('3' ))
7194 self .b3 .place (relx = 0.5 , rely = 0.6 , relwidth = 0.25 , relheight = 0.2 )
7295
73- self .bplus = Button (self .button_frame , text = '+' , command = lambda : self .put_symbol ( '+ ' ))
96+ self .bplus = Button (self .button_frame , text = '+' , command = lambda : self .operator ( ' + ' ))
7497 self .bplus .place (relx = 0.75 , rely = 0.6 , relwidth = 0.25 , relheight = 0.2 )
7598
7699 #Row 5 - last one
@@ -83,25 +106,71 @@ def create_buttons(self):
83106 self .bequal = Button (self .button_frame , text = '=' , command = self .equal )
84107 self .bequal .place (relx = 0.75 , rely = 0.8 , relwidth = 0.25 , relheight = 0.2 )
85108
86- def put_symbol ( self , symbol ):
87- self . equation = self . equation + symbol
109+ #updates the screen that shows the results and equations
110+ def update_screen ( self ):
88111 self .eqvar .set (self .equation )
89112
113+ #put a symbol at the screen
114+ def put_symbol (self , symbol ):
115+ if (not self .error ):
116+ self .equation = self .equation + symbol
117+ self .update_screen ()
118+
119+ def operator (self , symbol ):
120+ if (not self .error ):
121+ self .ponto = True
122+ self .put_symbol (symbol )
123+
90124 def put_dot (self ):
91- print ('I need to check if the dot already exists' )
125+ if (not self .error ):
126+ if (self .ponto ):
127+ self .put_symbol ('.' )
128+ self .ponto = False
129+
130+ def erase (self ):
131+ if (not self .error ):
132+ if self .equation [- 1 ] == '.' :
133+ self .ponto = True
134+ self .equation = self .equation [:- 1 ]
135+ self .update_screen ()
92136
93137 def clean (self ):
94138 self .equation = ''
95- self .eqvar .set (self .equation )
139+ self .error = False
140+ self .ponto = True
141+ self .lastvar .set ('' )
142+ self .update_screen ()
96143
97144 def equal (self ):
98- if (self .equation == '' ):
99- self .equation = '0'
100-
101- else :
102- self .equation = str (eval (self .equation ))
103-
104- self .eqvar .set (self .equation )
105-
145+ eq = self .equation
146+ if (not self .error ):
147+ if (self .equation == '' ):
148+ self .equation = '0'
149+
150+ else :
151+ new_list = []
152+ for e in self .equation .split ():
153+ a = e .lstrip ('0' )
154+ if a == '' :
155+ new_list .append ('0' )
156+ else :
157+ new_list .append (a )
158+
159+ try :
160+ self .equation = str (eval ('' .join (new_list )))
161+
162+ except ZeroDivisionError :
163+ if (not self .error ):
164+ self .equation = 'Divisão por zero'
165+ self .error = True
166+
167+ except SyntaxError :
168+ if (not self .error ):
169+ self .equation = 'Expressão mal escrita'
170+ self .error = True
171+
172+ finally :
173+ self .lastvar .set (eq )
174+ self .update_screen ()
106175
107176Calculadora ()
0 commit comments