1+ from kivy .app import App
2+ from kivy .uix .button import Button
3+ from kivy .uix .boxlayout import BoxLayout
4+ from kivy .uix .gridlayout import GridLayout
5+ from kivy .uix .label import Label
6+
7+ class myApp (App ):
8+ def build (self ):
9+ root_widget = BoxLayout (orientation = 'vertical' )
10+ output_label = Label (size_hint_y = 0.75 , font_size = 50 )
11+ button_symbols = ('1' , '2' , '3' , '+' ,
12+ '4' , '5' , '6' , '-' ,
13+ '7' , '8' , '9' , '.' ,
14+ '0' , '*' , '/' , '=' )
15+ button_grid = GridLayout (cols = 4 , size_hint_y = 2 )
16+ for symbol in button_symbols :
17+ button_grid .add_widget (Button (text = symbol ))
18+
19+ clear_button = Button (text = 'Clear' , size_hint_y = None , height = 100 )
20+ def print_button_text (instance ):
21+ output_label .text += instance .text
22+ for button in button_grid .children [1 :]:
23+ button .bind (on_press = print_button_text )
24+ def resize_label_text (label , new_height ):
25+ label .fontsize = 0.5 * label .height
26+ output_label .bind (height = resize_label_text )
27+
28+ def evaluate_result (instance ):
29+ try :
30+ output_label .text = str (eval (output_label .text ))
31+ except SyntaxError :
32+ output_label .text = 'Python Syntax error!'
33+ button_grid .children [0 ].bind (on_press = evaluate_result )
34+
35+ def clear_label (instance ):
36+ output_label .text = " "
37+ clear_button .bind (on_press = clear_label )
38+
39+ root_widget .add_widget (output_label )
40+ root_widget .add_widget (button_grid )
41+ root_widget .add_widget (clear_button )
42+ return root_widget
43+ myApp ().run ()
0 commit comments