Skip to content

Commit eb73753

Browse files
Conflicts: sudoku-chb.py
2 parents 1d84854 + fe316b2 commit eb73753

File tree

6 files changed

+224
-3
lines changed

6 files changed

+224
-3
lines changed
File renamed without changes.

AutoSizeBitmap.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
AutoSizeBitmap.py
5+
6+
Example for how to have a bitmap autosize itself in wxPython
7+
"""
8+
9+
import wx
10+
print "using wxPython version:", wx.__version__
11+
12+
class AutoSizeBitmap(wx.Window):
13+
"""
14+
A subclass of wx.Window that will hold an image (much like a StaticBitmap),
15+
but re-size it to fit the current size of the Window
16+
" """
17+
def __init__(self, parent, image, *args, **kwargs):
18+
"""
19+
initialize an AutoSizeBitmap
20+
21+
:param parent: parent Window for this window
22+
:param image: a wx.Image that you want to display
23+
"""
24+
wx.Window.__init__(self, parent, *args, **kwargs)
25+
26+
self.orig_image = image
27+
self.bitmap = None
28+
29+
self.Bind(wx.EVT_SIZE, self.OnSize)
30+
self.Bind(wx.EVT_PAINT, self.OnPaint)
31+
32+
def OnSize(self, evt=None):
33+
img = self.orig_image.Copy()
34+
img.Rescale(*self.Size)
35+
self.bitmap = wx.BitmapFromImage(img)
36+
self.Refresh()
37+
38+
def OnPaint(self, evt=None):
39+
dc = wx.PaintDC(self)
40+
try:
41+
dc.DrawBitmap(self.bitmap,0,0)
42+
except ValueError: # in case bitmap has not yet been initialized
43+
pass
44+
45+
if __name__ == "__main__":
46+
import sys
47+
48+
try:
49+
filename = sys.argv[1]
50+
except:
51+
filename = "Images/cute_close_up.jpg"
52+
App = wx.App(False)
53+
f = wx.Frame(None)
54+
img = wx.Image(filename)
55+
b = AutoSizeBitmap(f, img)
56+
f.Show()
57+
App.MainLoop()
58+

DrawingWhileMouseMoves.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
A test of drawing while the mouse is moving.
5+
6+
The old way of doing this is with a wxClientDC, but that does
7+
not work right, or at least not well on OS-X. So this shows how
8+
to do it with Refresh();Update().
9+
"""
10+
11+
import wx
12+
13+
print "running with version:", wx.__version__
14+
import random
15+
16+
17+
class DrawWindow(wx.Window):
18+
def __init__(self, parent, id = -1):
19+
## Any data the Draw() function needs must be initialized before
20+
## calling BufferedWindow.__init__, as it will call the Draw
21+
## function.
22+
wx.Window.__init__(self, parent, id)
23+
24+
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
25+
self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
26+
self.Bind(wx.EVT_MOTION, self.OnMove)
27+
28+
wx.EVT_PAINT(self, self.OnPaint)
29+
wx.EVT_SIZE(self, self.OnSize)
30+
31+
# OnSize called to make sure the buffer is initialized.
32+
# This might result in OnSize getting called twice on some
33+
# platforms at initialization, but little harm done.
34+
35+
self.mouse_line = None
36+
self.OnSize(None)
37+
38+
def OnPaint(self, event):
39+
# This draws the buffer to the screen, then optionally the mouse_line
40+
dc = wx.PaintDC(self)
41+
dc.DrawBitmap(self._Buffer,0,0)
42+
if self.mouse_line is not None:
43+
dc.SetPen(wx.Pen('WHITE', 2, wx.SHORT_DASH))
44+
dc.DrawLinePoint( *self.mouse_line )
45+
46+
def OnSize(self,event):
47+
# The Buffer init is done here, to make sure the buffer is always
48+
# the same size as the Window
49+
Size = self.GetClientSizeTuple()
50+
51+
# Make sure we don't try to create a 0 size bitmap
52+
Size = (max(Size[0], 1), max(Size[1], 1))
53+
self._Buffer = wx.EmptyBitmap(Size[0],Size[1])
54+
self.Draw()
55+
56+
def Draw(self):
57+
"""
58+
This draws the backgound image on the buffer
59+
60+
"""
61+
# update the buffer
62+
dc = wx.MemoryDC()
63+
dc.SelectObject(self._Buffer)
64+
65+
coords = ((40,40),(200,220),(210,120),(120,300))
66+
dc.BeginDrawing()
67+
dc.SetBackground( wx.Brush("Blue") )
68+
dc.Clear() # make sure you clear the bitmap!
69+
70+
dc.SetPen(wx.RED_PEN)
71+
dc.SetBrush(wx.CYAN_BRUSH)
72+
73+
dc.DrawPolygon(coords)
74+
75+
76+
def OnLeftDown(self, event):
77+
self.CaptureMouse()
78+
self.mouse_line = [ event.GetPosition(), None]
79+
80+
def OnLeftUp(self, event):
81+
self.mouse_line = None
82+
if self.HasCapture():
83+
self.ReleaseMouse()
84+
self.Refresh()
85+
self.Update()
86+
87+
def OnMove(self, event):
88+
if event.Dragging() and event.LeftIsDown() and (self.mouse_line is not None):
89+
self.mouse_line[1] = event.GetPosition()
90+
self.Refresh()
91+
## note: "Update() is not recommended on wxMac -- but response is slower without it... "
92+
#self.Update()
93+
94+
95+
if __name__ == "__main__":
96+
app = wx.App(False)
97+
frame = wx.Frame(None, size = (400,500), title="Mouse Move Drawing Test")
98+
draw_window = DrawWindow(frame)
99+
frame.Show(True)
100+
app.MainLoop()
101+
102+
103+
104+
105+
106+
107+
108+
109+
110+
111+
112+
113+
114+
115+
116+
117+
118+
119+
120+

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

sudoku-chb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ def test_PuzzleGrid():
145145

146146
class Grid:
147147
def __init__(self, w, h):
148-
size = max( 100, min(w,h) )
149-
self.d = d = (size - 20) / 9
148+
size = min(w,h)
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)

0 commit comments

Comments
 (0)