Added the sample code from wxPython In Action to the samples dir

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@42925 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2006-11-01 22:36:23 +00:00
parent bb2775b9e8
commit be05b43451
184 changed files with 9122 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
import wx
import wx.grid
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Grid Attributes",
size=(600,300))
grid = wx.grid.Grid(self)
grid.CreateGrid(10,6)
for row in range(10):
for col in range(6):
grid.SetCellValue(row, col, "(%s,%s)" % (row, col))
grid.SetCellTextColour(1, 1, "red")
grid.SetCellFont(1,1, wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))
grid.SetCellBackgroundColour(2, 2, "light blue")
attr = wx.grid.GridCellAttr()
attr.SetTextColour("navyblue")
attr.SetBackgroundColour("pink")
attr.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))
grid.SetAttr(4, 0, attr)
grid.SetAttr(5, 1, attr)
grid.SetRowAttr(8, attr)
app = wx.PySimpleApp()
frame = TestFrame()
frame.Show()
app.MainLoop()

View File

@@ -0,0 +1,18 @@
import wx
import wx.grid
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Simple Grid",
size=(640,480))
grid = wx.grid.Grid(self)
grid.CreateGrid(50,50)
for row in range(20):
for col in range(6):
grid.SetCellValue(row, col,
"cell (%d,%d)" % (row, col))
app = wx.PySimpleApp()
frame = TestFrame()
frame.Show()
app.MainLoop()

View File

@@ -0,0 +1,111 @@
import wx
import wx.grid
import string
class UpCaseCellEditor(wx.grid.PyGridCellEditor):
def __init__(self):
wx.grid.PyGridCellEditor.__init__(self)
def Create(self, parent, id, evtHandler):
"""
Called to create the control, which must derive from wx.Control.
*Must Override*
"""
self._tc = wx.TextCtrl(parent, id, "")
self._tc.SetInsertionPoint(0)
self.SetControl(self._tc)
if evtHandler:
self._tc.PushEventHandler(evtHandler)
self._tc.Bind(wx.EVT_CHAR, self.OnChar)
def SetSize(self, rect):
"""
Called to position/size the edit control within the cell rectangle.
If you don't fill the cell (the rect) then be sure to override
PaintBackground and do something meaningful there.
"""
self._tc.SetDimensions(rect.x, rect.y, rect.width+2, rect.height+2,
wx.SIZE_ALLOW_MINUS_ONE)
def BeginEdit(self, row, col, grid):
"""
Fetch the value from the table and prepare the edit control
to begin editing. Set the focus to the edit control.
*Must Override*
"""
self.startValue = grid.GetTable().GetValue(row, col)
self._tc.SetValue(self.startValue)
self._tc.SetInsertionPointEnd()
self._tc.SetFocus()
self._tc.SetSelection(0, self._tc.GetLastPosition())
def EndEdit(self, row, col, grid):
"""
Complete the editing of the current cell. Returns True if the value
has changed. If necessary, the control may be destroyed.
*Must Override*
"""
changed = False
val = self._tc.GetValue()
if val != self.startValue:
changed = True
grid.GetTable().SetValue(row, col, val) # update the table
self.startValue = ''
self._tc.SetValue('')
return changed
def Reset(self):
"""
Reset the value in the control back to its starting value.
*Must Override*
"""
self._tc.SetValue(self.startValue)
self._tc.SetInsertionPointEnd()
def Clone(self):
"""
Create a new object which is the copy of this one
*Must Override*
"""
return UpCaseCellEditor()
def StartingKey(self, evt):
"""
If the editor is enabled by pressing keys on the grid, this will be
called to let the editor do something about that first key if desired.
"""
self.OnChar(evt)
if evt.GetSkipped():
self._tc.EmulateKeyPress(evt)
def OnChar(self, evt):
key = evt.GetKeyCode()
if key > 255:
evt.Skip()
return
char = chr(key)
if char in string.letters:
char = char.upper()
self._tc.WriteText(char)
else:
evt.Skip()
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Grid Editor",
size=(640,480))
grid = wx.grid.Grid(self)
grid.CreateGrid(50,50)
grid.SetDefaultEditor(UpCaseCellEditor())
app = wx.PySimpleApp()
frame = TestFrame()
frame.Show()
app.MainLoop()

View File

@@ -0,0 +1,24 @@
import wx
import wx.grid
class TestFrame(wx.Frame):
rowLabels = ["uno", "dos", "tres", "quatro", "cinco"]
colLabels = ["homer", "marge", "bart", "lisa", "maggie"]
def __init__(self):
wx.Frame.__init__(self, None, title="Grid Headers",
size=(500,200))
grid = wx.grid.Grid(self)
grid.CreateGrid(5,5)
for row in range(5):
grid.SetRowLabelValue(row, self.rowLabels[row])
grid.SetColLabelValue(row, self.colLabels[row])
for col in range(5):
grid.SetCellValue(row, col,
"(%s,%s)" % (self.rowLabels[row], self.colLabels[col]))
app = wx.PySimpleApp()
frame = TestFrame()
frame.Show()
app.MainLoop()

View File

@@ -0,0 +1,59 @@
import wx
import wx.grid
import random
class RandomBackgroundRenderer(wx.grid.PyGridCellRenderer):
def __init__(self):
wx.grid.PyGridCellRenderer.__init__(self)
def Draw(self, grid, attr, dc, rect, row, col, isSelected):
text = grid.GetCellValue(row, col)
hAlign, vAlign = attr.GetAlignment()
dc.SetFont( attr.GetFont() )
if isSelected:
bg = grid.GetSelectionBackground()
fg = grid.GetSelectionForeground()
else:
bg = random.choice(["pink", "sky blue", "cyan", "yellow", "plum"])
fg = attr.GetTextColour()
dc.SetTextBackground(bg)
dc.SetTextForeground(fg)
dc.SetBrush(wx.Brush(bg, wx.SOLID))
dc.SetPen(wx.TRANSPARENT_PEN)
dc.DrawRectangleRect(rect)
grid.DrawTextRectangle(dc, text, rect, hAlign, vAlign)
def GetBestSize(self, grid, attr, dc, row, col):
text = grid.GetCellValue(row, col)
dc.SetFont(attr.GetFont())
w, h = dc.GetTextExtent(text)
return wx.Size(w, h)
def Clone(self):
return RandomBackgroundRenderer()
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Grid Renderer",
size=(640,480))
grid = wx.grid.Grid(self)
grid.CreateGrid(50,50)
# Set this custom renderer just for row 4
attr = wx.grid.GridCellAttr()
attr.SetRenderer(RandomBackgroundRenderer())
grid.SetRowAttr(4, attr)
for row in range(10):
for col in range(10):
grid.SetCellValue(row, col,
"cell (%d,%d)" % (row, col))
app = wx.PySimpleApp()
frame = TestFrame()
frame.Show()
app.MainLoop()

View File

@@ -0,0 +1,23 @@
import wx
import wx.grid
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Grid Sizes",
size=(600,300))
grid = wx.grid.Grid(self)
grid.CreateGrid(5,5)
for row in range(5):
for col in range(5):
grid.SetCellValue(row, col, "(%s,%s)" % (row, col))
grid.SetCellSize(2, 2, 2, 3)
grid.SetColSize(1, 125)
grid.SetRowSize(1, 100)
app = wx.PySimpleApp()
frame = TestFrame()
frame.Show()
app.MainLoop()

View File

@@ -0,0 +1,65 @@
import wx
import wx.grid
class TestTable(wx.grid.PyGridTableBase):
def __init__(self):
wx.grid.PyGridTableBase.__init__(self)
self.data = { (1,1) : "Here",
(2,2) : "is",
(3,3) : "some",
(4,4) : "data",
}
self.odd=wx.grid.GridCellAttr()
self.odd.SetBackgroundColour("sky blue")
self.odd.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))
self.even=wx.grid.GridCellAttr()
self.even.SetBackgroundColour("sea green")
self.even.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))
# these five are the required methods
def GetNumberRows(self):
return 50
def GetNumberCols(self):
return 50
def IsEmptyCell(self, row, col):
return self.data.get((row, col)) is not None
def GetValue(self, row, col):
value = self.data.get((row, col))
if value is not None:
return value
else:
return ''
def SetValue(self, row, col, value):
self.data[(row,col)] = value
# the table can also provide the attribute for each cell
def GetAttr(self, row, col, kind):
attr = [self.even, self.odd][row % 2]
attr.IncRef()
return attr
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Grid Table",
size=(640,480))
grid = wx.grid.Grid(self)
table = TestTable()
grid.SetTable(table, True)
app = wx.PySimpleApp()
frame = TestFrame()
frame.Show()
app.MainLoop()

View File

@@ -0,0 +1,42 @@
import wx
import wx.grid
class TestTable(wx.grid.PyGridTableBase):
def __init__(self):
wx.grid.PyGridTableBase.__init__(self)
self.rowLabels = ["uno", "dos", "tres", "quatro", "cinco"]
self.colLabels = ["homer", "marge", "bart", "lisa", "maggie"]
def GetNumberRows(self):
return 5
def GetNumberCols(self):
return 5
def IsEmptyCell(self, row, col):
return False
def GetValue(self, row, col):
return "(%s,%s)" % (self.rowLabels[row], self.colLabels[col])
def SetValue(self, row, col, value):
pass
def GetColLabelValue(self, col):
return self.colLabels[col]
def GetRowLabelValue(self, row):
return self.rowLabels[row]
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Grid Table",
size=(500,200))
grid = wx.grid.Grid(self)
table = TestTable()
grid.SetTable(table, True)
app = wx.PySimpleApp()
frame = TestFrame()
frame.Show()
app.MainLoop()

View File

@@ -0,0 +1,64 @@
import wx
import wx.grid
class TestTable(wx.grid.PyGridTableBase):
def __init__(self):
wx.grid.PyGridTableBase.__init__(self)
self.data = { (1,1) : "Here",
(2,2) : "is",
(3,3) : "some",
(4,4) : "data",
}
self.odd=wx.grid.GridCellAttr()
self.odd.SetBackgroundColour("sky blue")
self.odd.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))
self.even=wx.grid.GridCellAttr()
self.even.SetBackgroundColour("sea green")
self.even.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))
# these five are the required methods
def GetNumberRows(self):
return 50
def GetNumberCols(self):
return 50
def IsEmptyCell(self, row, col):
return self.data.get((row, col)) is not None
def GetValue(self, row, col):
value = self.data.get((row, col))
if value is not None:
return value
else:
return ''
def SetValue(self, row, col, value):
self.data[(row,col)] = value
# the table can also provide the attribute for each cell
def GetAttr(self, row, col, kind):
attr = [self.even, self.odd][row % 2]
attr.IncRef()
return attr
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Grid Table",
size=(640,480))
grid = wx.grid.Grid(self)
table = TestTable()
grid.SetTable(table, True)
app = wx.PySimpleApp()
frame = TestFrame()
frame.Show()
app.MainLoop()