11#!/usr/bin/env python
2-
32"""
43wxPython Calculator Demo in 50 lines of code
54
1110It has been altered to allow it to be "driven" by an external script,
1211plus a little layout improvement
1312
14- See CalcualtorDemoDriver.py
13+ See CalcualtorDemoDriver.py
1514
1615for an example
1716"""
1817
19-
2018# Calculator GUI:
2119
2220# ___________v
23- # [7][8][9][/]
21+ # [7][8][9][/]
2422# [4][5][6][*]
2523# [1][2][3][-]
2624# [0][.][C][+]
2725# [ = ]
2826
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 )
3028import wx
31- from math import * # So we can evaluate "sqrt(8)"
29+ from math import * # So we can evaluate "sqrt(8)" and others
3230
3331
3432class Calculator (wx .Panel ):
3533 '''Main calculator dialog'''
34+
3635 def __init__ (self , * args , ** kwargs ):
3736 wx .Panel .__init__ (self , * args , ** kwargs )
38- sizer = wx .BoxSizer (wx .VERTICAL ) # Main vertical sizer
37+ sizer = wx .BoxSizer (wx .VERTICAL ) # Main vertical sizer
3938
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
4242
43- # [7][8][9][/]
43+ # [7][8][9][/]
4444 # [4][5][6][*]
4545 # [1][2][3][-]
4646 # [0][.][C][+]
4747 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" , "+" )):
5250 for label in row :
53- b = wx .Button (self , label = label , size = (40 ,- 1 ))
51+ b = wx .Button (self , label = label , size = (40 , - 1 ))
5452 gsizer .Add (b )
5553 b .Bind (wx .EVT_BUTTON , self .OnButton )
5654 sizer .Add (gsizer , 1 , wx .EXPAND )
5755
5856 # [ = ]
5957 b = wx .Button (self , label = "=" )
6058 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 )
6260 self .equal = b
6361
6462 # Set sizer and center
6563 self .SetSizerAndFit (sizer )
6664
6765 def OnButton (self , evt ):
6866 '''Handle button click event'''
69-
67+
7068 # Get title of clicked button
7169 label = evt .GetEventObject ().GetLabel ()
7270
73- if label == "=" : # Calculate
71+ if label == "=" : # Calculate
7472 self .Calculate ()
75- elif label == "C" : # Clear
73+ elif label == "C" : # Clear
7674 self .display .SetValue ("" )
7775
78- else : # Just add button text to current calculation
76+ else : # Just add button text to current calculation
7977 self .display .SetValue (self .display .GetValue () + label )
8078 self .display .SetInsertionPointEnd ()
81- self .equal .SetFocus () # Set the [=] button in focus
79+ self .equal .SetFocus () # Set the [=] button in focus
8280
8381 def Calculate (self ):
8482 """
8583 do the calculation itself
86-
84+
8785 in a separate method, so it can be called outside of a button event handler
8886 """
8987 try :
@@ -100,20 +98,21 @@ def Calculate(self):
10098
10199 # Show result
102100 self .display .SetValue (str (result ))
103- except Exception , e :
101+ except Exception as e :
104102 wx .LogError (str (e ))
105103 return
106104
107105 def ComputeExpression (self , expression ):
108106 """
109107 Compute the expression passed in.
110-
108+
111109 This can be called from another class, module, etc.
112110 """
113- print "ComputeExpression called with:" , expression
111+ print ( "ComputeExpression called with:" , expression )
114112 self .display .SetValue (expression )
115113 self .Calculate ()
116114
115+
117116class MainFrame (wx .Frame ):
118117 def __init__ (self , * args , ** kwargs ):
119118 kwargs .setdefault ('title' , "Calculator" )
@@ -123,7 +122,7 @@ def __init__(self, *args, **kwargs):
123122
124123 # put the panel on -- in a sizer to give it some space
125124 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 )
127126 self .SetSizerAndFit (S )
128127 self .CenterOnScreen ()
129128
@@ -134,4 +133,3 @@ def __init__(self, *args, **kwargs):
134133 frame = MainFrame (None )
135134 frame .Show ()
136135 app .MainLoop ()
137-
0 commit comments