Skip to content

Commit b64f133

Browse files
2 parents 48d05cf + eb73753 commit b64f133

File tree

3 files changed

+43
-17
lines changed

3 files changed

+43
-17
lines changed

00-README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ http://wiki.wxpython.org/index.cgi/wxPython_Style_Guide
2424
Despite these flaws, I think there is a lot to be learned from this
2525
collection.
2626

27-
I suggest you search or grep for a widget you may want to learn somethign about, or just fire up each one and see what it does!
27+
I suggest you search or grep for a widget you may want to learn something about, or just fire up each one and see what it does!
2828

2929
To Do
3030
----------

CalculatorDemo.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44
wxPython 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
88
http://wiki.wxpython.org/CalculatorDemo
99
by Miki Tebeka
@@ -30,14 +30,15 @@
3030
import wx
3131
from 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+
116131
if __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+

CalculatorDemoDriver.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
#!/usr/bin/env python
22

33
"""
4-
demo of how to drive a wxPython app from another script:
4+
NOTE: This worked when it was written, but not with recent (2.9)
5+
versions of wxPython. I suspect that it would have to be re-factored
6+
to put the GUI in the main thread, and star a secondary thread to
7+
interact with the user.
8+
9+
Demo of how to drive a wxPython app from another script:
510
611
one thread to do the driving, one thread to run the Calculator:
712
813
the primary thread runs the GUI - - it gets "locked up" with the mainloop.
914
10-
the secondary thread i used to do things in a way outside the GUI. In this
15+
the secondary thread is used to do things outside the GUI. In this
1116
case, a simple pause and sending commands now and then. The commands are put
1217
on the event loop with a wx.CallAfter() call.
1318
@@ -33,7 +38,7 @@ def run(self):
3338
"""
3439
#Create the application
3540
self.app = wx.App(False)
36-
self.calculator = CalculatorDemo.Calculator()
41+
self.calculator = CalculatorDemo.MainFrame(None)
3742
self.calculator.Show()
3843
self.app.MainLoop()
3944

@@ -42,12 +47,17 @@ def run(self):
4247
gui_thread = GUI_Thread()
4348
gui_thread.start()
4449

50+
# the computer object:
51+
computer = gui_thread.calculator.calcPanel.ComputeExpression
52+
4553
# now we have control back -- start a loop for user input
46-
print "enter expressions to calcualte: enter to evaluate"
54+
print "enter expressions to calculate: enter to evaluate"
4755
print "hit ctrl+C to exit"
4856
while True:
4957
expr = raw_input()
5058
# send the input to the calculator to calculate
51-
gui_thread.calculator.ComputeExpression(expr)
59+
print "calling computer with:", expr
60+
wx.CallAfter(computer, expr)
61+
5262

5363

0 commit comments

Comments
 (0)