33"""
44wxPython Calculator Demo in 50 lines of code
55
6- This demo was pulled form teh wxPython Wiki:
6+ This demo was pulled from the wxPython Wiki:
77
88http://wiki.wxpython.org/CalculatorDemo
99by Miki Tebeka
3030import wx
3131from math import * # So we can evaluate "sqrt(8)"
3232
33- class Calculator (wx .Frame ):
33+
34+ class Calculator (wx .Panel ):
3435 '''Main calculator dialog'''
35- def __init__ (self , parent = None ):
36- wx .Frame .__init__ (self , parent , title = "Calculator" )
36+ def __init__ (self , * args , ** kwargs ):
37+ wx .Panel .__init__ (self , * args , ** kwargs )
3738 sizer = wx .BoxSizer (wx .VERTICAL ) # Main vertical sizer
3839
39- self .display = wx .ComboBox (self , - 1 ) # Current calculation
40- sizer .Add (self .display , 0 , wx .EXPAND ) # Add to main sizer
40+ self .display = wx .ComboBox (self ) # Current calculation
41+ sizer .Add (self .display , 0 , wx .EXPAND | wx . BOTTOM , 8 ) # Add to main sizer
4142
4243 # [7][8][9][/]
4344 # [4][5][6][*]
@@ -61,9 +62,7 @@ def __init__(self, parent=None):
6162 self .equal = b
6263
6364 # Set sizer and center
64- self .SetSizer (sizer )
65- sizer .Fit (self )
66- self .CenterOnScreen ()
65+ self .SetSizerAndFit (sizer )
6766
6867 def OnButton (self , evt ):
6968 '''Handle button click event'''
@@ -78,6 +77,7 @@ def OnButton(self, evt):
7877
7978 else : # Just add button text to current calculation
8079 self .display .SetValue (self .display .GetValue () + label )
80+ self .display .SetInsertionPointEnd ()
8181 self .equal .SetFocus () # Set the [=] button in focus
8282
8383 def Calculate (self ):
@@ -110,12 +110,28 @@ def ComputeExpression(self, expression):
110110
111111 This can be called from another class, module, etc.
112112 """
113+ print "ComputeExpression called with:" , expression
113114 self .display .SetValue (expression )
114115 self .Calculate ()
115116
117+ class MainFrame (wx .Frame ):
118+ def __init__ (self , * args , ** kwargs ):
119+ kwargs .setdefault ('title' , "Calculator" )
120+ wx .Frame .__init__ (self , * args , ** kwargs )
121+
122+ self .calcPanel = Calculator (self )
123+
124+ # put the panel on -- in a sizer to give it some space
125+ S = wx .BoxSizer (wx .VERTICAL )
126+ S .Add (self .calcPanel , 1 , wx .GROW | wx .ALL , 10 )
127+ self .SetSizerAndFit (S )
128+ self .CenterOnScreen ()
129+
130+
116131if __name__ == "__main__" :
117132 # Run the application
118133 app = wx .App (False )
119- frame = Calculator (None )
134+ frame = MainFrame (None )
120135 frame .Show ()
121- app .MainLoop ()
136+ app .MainLoop ()
137+
0 commit comments