Skip to content

Commit e188668

Browse files
2 parents dfdbee1 + b64f133 commit e188668

File tree

7 files changed

+98
-22
lines changed

7 files changed

+98
-22
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

JustButton.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python
2+
3+
import wx
4+
import wx.lib.buttons
5+
6+
class ButtonPanel(wx.Panel):
7+
def __init__(self, *args, **kwargs):
8+
wx.Panel.__init__(self, *args, **kwargs)
9+
10+
btn = wx.Button(self, label = "Push Me", pos=(40,60), size=(120,-1))
11+
btn = wx.Button(self, label = "Push Me Also", pos=(80,30), size=(200, 60))
12+
btn = wx.Button(self, label = "No, push me!", pos=(90,110), size=(65, 80))
13+
btn = wx.lib.buttons.GenButton(self, label = "better?", pos=(40,150), size=(60, 60))
14+
15+
btn.Bind(wx.EVT_BUTTON, self.OnButton )
16+
17+
def OnButton(self, evt):
18+
print "Button Clicked"
19+
20+
21+
class DemoFrame(wx.Frame):
22+
""" This frame displays a panel with a button] """
23+
def __init__(self, title = "Micro App"):
24+
wx.Frame.__init__(self, None , -1, title)
25+
26+
pnl = ButtonPanel(self)
27+
28+
self.Bind(wx.EVT_CLOSE, self.OnQuit)
29+
30+
31+
def OnQuit(self,Event):
32+
self.Destroy()
33+
34+
35+
app = wx.App(False)
36+
frame = DemoFrame()
37+
frame.Show()
38+
app.MainLoop()
39+
40+
41+
42+
43+

MicroApp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def __init__(self, title = "Micro App"):
3939

4040
self.SetMenuBar(MenuBar)
4141

42-
btn = wx.Button(self, label = "Quit")
42+
btn = wx.Button(self, label = "Quit", pos=(40,60))
4343

4444
btn.Bind(wx.EVT_BUTTON, self.OnQuit )
4545

OffScreenHTML.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
"""
44
test of rendering HTML to an off-screen bitmap
5+
6+
This version uses a wx.GCDC, so you can have an alpha background.
7+
8+
Works on OS-X, may need an explicite alpha bitmap on other platforms
59
"""
610

711
import wx
@@ -17,7 +21,8 @@ def __init__(self, width, height):
1721
self.HR = wx.html.HtmlDCRenderer()
1822

1923
# a bunch of defaults...
20-
self.BackgroundColor = "White"
24+
self.BackgroundColor = "white"
25+
self.BackgroundColor = (255, 255, 255, 50)
2126
self.Padding = 10
2227

2328

@@ -27,6 +32,7 @@ def Render(self, source):
2732
"""
2833
DC = wx.MemoryDC()
2934
DC.SelectObject(self.Buffer)
35+
DC = wx.GCDC(DC)
3036
DC.SetBackground(wx.Brush(self.BackgroundColor))
3137
DC.Clear()
3238

@@ -78,7 +84,8 @@ def Convert(self, text):
7884
that will show if it can take care of the basic rendering for us, and
7985
auto-wrap, and all sorts of nifty stuff like that
8086
</p>
81-
87+
<p>
88+
and here is some <b> Bold Text </b>
8289
<p> It does seem to work OK </p>
8390
"""
8491

sudoku-chb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def test_PuzzleGrid():
146146
class Grid:
147147
def __init__(self, w, h):
148148
size = min(w,h)
149-
self.d = d = (size - 20) / 9
149+
self.d = d = max( 2, (size - 20) / 9 )# make sure we don't get zero...
150150
self.x0 = (w - (self.d * 9)) / 2
151151
self.y0 = (h - (self.d * 9)) / 2
152152
self.font_size = int(11 * d/16.0)
@@ -212,7 +212,7 @@ def Draw(self, dc):
212212
# draw the background:
213213
dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
214214
dc.Clear()
215-
dc.SetBrush(wx.Brush(wx.Color(128,128,255)))
215+
dc.SetBrush(wx.Brush(wx.Colour(128,128,255)))
216216
dc.SetPen(wx.TRANSPARENT_PEN)
217217
dc.DrawRectangle(x0, y0, d*9, d*9 )
218218

0 commit comments

Comments
 (0)