1
1
#!/usr/bin/env python
2
-
3
2
"""
4
3
wxPython Calculator Demo in 50 lines of code
5
4
11
10
It has been altered to allow it to be "driven" by an external script,
12
11
plus a little layout improvement
13
12
14
- See CalcualtorDemoDriver.py
13
+ See CalcualtorDemoDriver.py
15
14
16
15
for an example
17
16
"""
18
17
19
-
20
18
# Calculator GUI:
21
19
22
20
# ___________v
23
- # [7][8][9][/]
21
+ # [7][8][9][/]
24
22
# [4][5][6][*]
25
23
# [1][2][3][-]
26
24
# [0][.][C][+]
27
25
# [ = ]
28
26
29
- from __future__ import division # So that 8/3 will be 2.6666 and not 2
27
+ from __future__ import ( division , unicode_literals , print_function )
30
28
import wx
31
- from math import * # So we can evaluate "sqrt(8)"
29
+ from math import * # So we can evaluate "sqrt(8)" and others
32
30
33
31
34
32
class Calculator (wx .Panel ):
35
33
'''Main calculator dialog'''
34
+
36
35
def __init__ (self , * args , ** kwargs ):
37
36
wx .Panel .__init__ (self , * args , ** kwargs )
38
- sizer = wx .BoxSizer (wx .VERTICAL ) # Main vertical sizer
37
+ sizer = wx .BoxSizer (wx .VERTICAL ) # Main vertical sizer
39
38
40
- self .display = wx .ComboBox (self ) # Current calculation
41
- sizer .Add (self .display , 0 , wx .EXPAND | wx .BOTTOM , 8 ) # Add to main sizer
39
+ self .display = wx .ComboBox (self ) # Current calculation
40
+ sizer .Add (self .display , 0 , wx .EXPAND | wx .BOTTOM ,
41
+ 8 ) # Add to main sizer
42
42
43
- # [7][8][9][/]
43
+ # [7][8][9][/]
44
44
# [4][5][6][*]
45
45
# [1][2][3][-]
46
46
# [0][.][C][+]
47
47
gsizer = wx .GridSizer (4 , 4 , 8 , 8 )
48
- for row in (("7" , "8" , "9" , "/" ),
49
- ("4" , "5" , "6" , "*" ),
50
- ("1" , "2" , "3" , "-" ),
51
- ("0" , "." , "C" , "+" )):
48
+ for row in (("7" , "8" , "9" , "/" ), ("4" , "5" , "6" , "*" ),
49
+ ("1" , "2" , "3" , "-" ), ("0" , "." , "C" , "+" )):
52
50
for label in row :
53
- b = wx .Button (self , label = label , size = (40 ,- 1 ))
51
+ b = wx .Button (self , label = label , size = (40 , - 1 ))
54
52
gsizer .Add (b )
55
53
b .Bind (wx .EVT_BUTTON , self .OnButton )
56
54
sizer .Add (gsizer , 1 , wx .EXPAND )
57
55
58
56
# [ = ]
59
57
b = wx .Button (self , label = "=" )
60
58
b .Bind (wx .EVT_BUTTON , self .OnButton )
61
- sizer .Add (b , 0 , wx .EXPAND | wx .ALL , 8 )
59
+ sizer .Add (b , 0 , wx .EXPAND | wx .ALL , 8 )
62
60
self .equal = b
63
61
64
62
# Set sizer and center
65
63
self .SetSizerAndFit (sizer )
66
64
67
65
def OnButton (self , evt ):
68
66
'''Handle button click event'''
69
-
67
+
70
68
# Get title of clicked button
71
69
label = evt .GetEventObject ().GetLabel ()
72
70
73
- if label == "=" : # Calculate
71
+ if label == "=" : # Calculate
74
72
self .Calculate ()
75
- elif label == "C" : # Clear
73
+ elif label == "C" : # Clear
76
74
self .display .SetValue ("" )
77
75
78
- else : # Just add button text to current calculation
76
+ else : # Just add button text to current calculation
79
77
self .display .SetValue (self .display .GetValue () + label )
80
78
self .display .SetInsertionPointEnd ()
81
- self .equal .SetFocus () # Set the [=] button in focus
79
+ self .equal .SetFocus () # Set the [=] button in focus
82
80
83
81
def Calculate (self ):
84
82
"""
85
83
do the calculation itself
86
-
84
+
87
85
in a separate method, so it can be called outside of a button event handler
88
86
"""
89
87
try :
@@ -100,20 +98,21 @@ def Calculate(self):
100
98
101
99
# Show result
102
100
self .display .SetValue (str (result ))
103
- except Exception , e :
101
+ except Exception as e :
104
102
wx .LogError (str (e ))
105
103
return
106
104
107
105
def ComputeExpression (self , expression ):
108
106
"""
109
107
Compute the expression passed in.
110
-
108
+
111
109
This can be called from another class, module, etc.
112
110
"""
113
- print "ComputeExpression called with:" , expression
111
+ print ( "ComputeExpression called with:" , expression )
114
112
self .display .SetValue (expression )
115
113
self .Calculate ()
116
114
115
+
117
116
class MainFrame (wx .Frame ):
118
117
def __init__ (self , * args , ** kwargs ):
119
118
kwargs .setdefault ('title' , "Calculator" )
@@ -123,7 +122,7 @@ def __init__(self, *args, **kwargs):
123
122
124
123
# put the panel on -- in a sizer to give it some space
125
124
S = wx .BoxSizer (wx .VERTICAL )
126
- S .Add (self .calcPanel , 1 , wx .GROW | wx .ALL , 10 )
125
+ S .Add (self .calcPanel , 1 , wx .GROW | wx .ALL , 10 )
127
126
self .SetSizerAndFit (S )
128
127
self .CenterOnScreen ()
129
128
@@ -134,4 +133,3 @@ def __init__(self, *args, **kwargs):
134
133
frame = MainFrame (None )
135
134
frame .Show ()
136
135
app .MainLoop ()
137
-
0 commit comments