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:
20
wxPython/samples/wxPIA_book/Chapter-11/basicflexgridsizer.py
Normal file
20
wxPython/samples/wxPIA_book/Chapter-11/basicflexgridsizer.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import wx
|
||||
from blockwindow import BlockWindow
|
||||
|
||||
labels = "one two three four five six seven eight nine".split()
|
||||
|
||||
class TestFrame(wx.Frame):
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, -1, "FlexGridSizer")
|
||||
sizer = wx.FlexGridSizer(rows=3, cols=3, hgap=5, vgap=5)
|
||||
for label in labels:
|
||||
bw = BlockWindow(self, label=label)
|
||||
sizer.Add(bw, 0, 0)
|
||||
center = self.FindWindowByName("five")
|
||||
center.SetMinSize((150,50))
|
||||
self.SetSizer(sizer)
|
||||
self.Fit()
|
||||
|
||||
app = wx.PySimpleApp()
|
||||
TestFrame().Show()
|
||||
app.MainLoop()
|
18
wxPython/samples/wxPIA_book/Chapter-11/basicgridsizer.py
Normal file
18
wxPython/samples/wxPIA_book/Chapter-11/basicgridsizer.py
Normal file
@@ -0,0 +1,18 @@
|
||||
import wx
|
||||
from blockwindow import BlockWindow
|
||||
|
||||
labels = "one two three four five six seven eight nine".split()
|
||||
|
||||
class GridSizerFrame(wx.Frame):
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, -1, "Basic Grid Sizer")
|
||||
sizer = wx.GridSizer(rows=3, cols=3, hgap=5, vgap=5)
|
||||
for label in labels:
|
||||
bw = BlockWindow(self, label=label)
|
||||
sizer.Add(bw, 0, 0)
|
||||
self.SetSizer(sizer)
|
||||
self.Fit()
|
||||
|
||||
app = wx.PySimpleApp()
|
||||
GridSizerFrame().Show()
|
||||
app.MainLoop()
|
20
wxPython/samples/wxPIA_book/Chapter-11/blockwindow.py
Normal file
20
wxPython/samples/wxPIA_book/Chapter-11/blockwindow.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import wx
|
||||
|
||||
class BlockWindow(wx.Panel):
|
||||
def __init__(self, parent, ID=-1, label="",
|
||||
pos=wx.DefaultPosition, size=(100, 25)):
|
||||
wx.Panel.__init__(self, parent, ID, pos, size,
|
||||
wx.RAISED_BORDER, label)
|
||||
self.label = label
|
||||
self.SetBackgroundColour("white")
|
||||
self.SetMinSize(size)
|
||||
self.Bind(wx.EVT_PAINT, self.OnPaint)
|
||||
|
||||
def OnPaint(self, evt):
|
||||
sz = self.GetClientSize()
|
||||
dc = wx.PaintDC(self)
|
||||
w,h = dc.GetTextExtent(self.label)
|
||||
dc.SetFont(self.GetFont())
|
||||
dc.DrawText(self.label, (sz.width-w)/2, (sz.height-h)/2)
|
||||
|
||||
|
23
wxPython/samples/wxPIA_book/Chapter-11/bordergridsizer.py
Normal file
23
wxPython/samples/wxPIA_book/Chapter-11/bordergridsizer.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import wx
|
||||
from blockwindow import BlockWindow
|
||||
|
||||
labels = "one two three four five six seven eight nine".split()
|
||||
flags = {"one": wx.BOTTOM, "two": wx.ALL, "three": wx.TOP,
|
||||
"four": wx.LEFT, "five": wx.ALL, "six": wx.RIGHT,
|
||||
"seven": wx.BOTTOM | wx.TOP, "eight": wx.ALL,
|
||||
"nine": wx.LEFT | wx.RIGHT}
|
||||
|
||||
class TestFrame(wx.Frame):
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, -1, "GridSizer Borders")
|
||||
sizer = wx.GridSizer(rows=3, cols=3, hgap=5, vgap=5)
|
||||
for label in labels:
|
||||
bw = BlockWindow(self, label=label)
|
||||
flag = flags.get(label, 0)
|
||||
sizer.Add(bw, 0, flag, 10)
|
||||
self.SetSizer(sizer)
|
||||
self.Fit()
|
||||
|
||||
app = wx.PySimpleApp()
|
||||
TestFrame().Show()
|
||||
app.MainLoop()
|
78
wxPython/samples/wxPIA_book/Chapter-11/boxsizer.py
Normal file
78
wxPython/samples/wxPIA_book/Chapter-11/boxsizer.py
Normal file
@@ -0,0 +1,78 @@
|
||||
import wx
|
||||
from blockwindow import BlockWindow
|
||||
|
||||
labels = "one two three four".split()
|
||||
|
||||
class TestFrame(wx.Frame):
|
||||
title = "none"
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, -1, self.title)
|
||||
sizer = self.CreateSizerAndWindows()
|
||||
self.SetSizer(sizer)
|
||||
self.Fit()
|
||||
|
||||
class VBoxSizerFrame(TestFrame):
|
||||
title = "Vertical BoxSizer"
|
||||
|
||||
def CreateSizerAndWindows(self):
|
||||
sizer = wx.BoxSizer(wx.VERTICAL)
|
||||
for label in labels:
|
||||
bw = BlockWindow(self, label=label, size=(200,30))
|
||||
sizer.Add(bw, flag=wx.EXPAND)
|
||||
return sizer
|
||||
|
||||
|
||||
class HBoxSizerFrame(TestFrame):
|
||||
title = "Horizontal BoxSizer"
|
||||
|
||||
def CreateSizerAndWindows(self):
|
||||
sizer = wx.BoxSizer(wx.HORIZONTAL)
|
||||
for label in labels:
|
||||
bw = BlockWindow(self, label=label, size=(75,30))
|
||||
sizer.Add(bw, flag=wx.EXPAND)
|
||||
return sizer
|
||||
|
||||
class VBoxSizerStretchableFrame(TestFrame):
|
||||
title = "Stretchable BoxSizer"
|
||||
|
||||
def CreateSizerAndWindows(self):
|
||||
sizer = wx.BoxSizer(wx.VERTICAL)
|
||||
for label in labels:
|
||||
bw = BlockWindow(self, label=label, size=(200,30))
|
||||
sizer.Add(bw, flag=wx.EXPAND)
|
||||
|
||||
# Add an item that takes all the free space
|
||||
bw = BlockWindow(self, label="gets all free space", size=(200,30))
|
||||
sizer.Add(bw, 1, flag=wx.EXPAND)
|
||||
return sizer
|
||||
|
||||
class VBoxSizerMultiProportionalFrame(TestFrame):
|
||||
title = "Proportional BoxSizer"
|
||||
|
||||
def CreateSizerAndWindows(self):
|
||||
sizer = wx.BoxSizer(wx.VERTICAL)
|
||||
for label in labels:
|
||||
bw = BlockWindow(self, label=label, size=(200,30))
|
||||
sizer.Add(bw, flag=wx.EXPAND)
|
||||
|
||||
# Add an item that takes one share of the free space
|
||||
bw = BlockWindow(self,
|
||||
label="gets 1/3 of the free space",
|
||||
size=(200,30))
|
||||
sizer.Add(bw, 1, flag=wx.EXPAND)
|
||||
|
||||
# Add an item that takes 2 shares of the free space
|
||||
bw = BlockWindow(self,
|
||||
label="gets 2/3 of the free space",
|
||||
size=(200,30))
|
||||
sizer.Add(bw, 2, flag=wx.EXPAND)
|
||||
return sizer
|
||||
|
||||
app = wx.PySimpleApp()
|
||||
frameList = [VBoxSizerFrame, HBoxSizerFrame,
|
||||
VBoxSizerStretchableFrame,
|
||||
VBoxSizerMultiProportionalFrame]
|
||||
for klass in frameList:
|
||||
frame = klass()
|
||||
frame.Show()
|
||||
app.MainLoop()
|
34
wxPython/samples/wxPIA_book/Chapter-11/gridbagsizer.py
Normal file
34
wxPython/samples/wxPIA_book/Chapter-11/gridbagsizer.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import wx
|
||||
from blockwindow import BlockWindow
|
||||
|
||||
labels = "one two three four five six seven eight nine".split()
|
||||
|
||||
class TestFrame(wx.Frame):
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, -1, "GridBagSizer Test")
|
||||
sizer = wx.GridBagSizer(hgap=5, vgap=5)
|
||||
for col in range(3):
|
||||
for row in range(3):
|
||||
bw = BlockWindow(self, label=labels[row*3 + col])
|
||||
sizer.Add(bw, pos=(row,col))
|
||||
|
||||
# add a window that spans several rows
|
||||
bw = BlockWindow(self, label="span 3 rows")
|
||||
sizer.Add(bw, pos=(0,3), span=(3,1), flag=wx.EXPAND)
|
||||
|
||||
# add a window that spans all columns
|
||||
bw = BlockWindow(self, label="span all columns")
|
||||
sizer.Add(bw, pos=(3,0), span=(1,4), flag=wx.EXPAND)
|
||||
|
||||
# make the last row and col be stretchable
|
||||
sizer.AddGrowableCol(3)
|
||||
sizer.AddGrowableRow(3)
|
||||
|
||||
self.SetSizer(sizer)
|
||||
self.Fit()
|
||||
|
||||
|
||||
app = wx.PySimpleApp()
|
||||
TestFrame().Show()
|
||||
|
||||
app.MainLoop()
|
19
wxPython/samples/wxPIA_book/Chapter-11/mingridsizer.py
Normal file
19
wxPython/samples/wxPIA_book/Chapter-11/mingridsizer.py
Normal file
@@ -0,0 +1,19 @@
|
||||
import wx
|
||||
from blockwindow import BlockWindow
|
||||
|
||||
labels = "one two three four five six seven eight nine".split()
|
||||
|
||||
class TestFrame(wx.Frame):
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, -1, "GridSizer Test")
|
||||
sizer = wx.GridSizer(rows=3, cols=3, hgap=5, vgap=5)
|
||||
for label in labels:
|
||||
bw = BlockWindow(self, label=label)
|
||||
sizer.Add(bw, 0, 0)
|
||||
center = self.FindWindowByName("five")
|
||||
center.SetMinSize((150,50))
|
||||
self.SetSizer(sizer)
|
||||
self.Fit()
|
||||
app = wx.PySimpleApp()
|
||||
TestFrame().Show()
|
||||
app.MainLoop()
|
18
wxPython/samples/wxPIA_book/Chapter-11/prependgridsizer.py
Normal file
18
wxPython/samples/wxPIA_book/Chapter-11/prependgridsizer.py
Normal file
@@ -0,0 +1,18 @@
|
||||
import wx
|
||||
from blockwindow import BlockWindow
|
||||
|
||||
labels = "one two three four five six seven eight nine".split()
|
||||
|
||||
class GridSizerFrame(wx.Frame):
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, -1, "Prepend Grid Sizer")
|
||||
sizer = wx.GridSizer(rows=3, cols=3, hgap=5, vgap=5)
|
||||
for label in labels:
|
||||
bw = BlockWindow(self, label=label)
|
||||
sizer.Prepend(bw, 0, 0)
|
||||
self.SetSizer(sizer)
|
||||
self.Fit()
|
||||
|
||||
app = wx.PySimpleApp()
|
||||
GridSizerFrame().Show()
|
||||
app.MainLoop()
|
95
wxPython/samples/wxPIA_book/Chapter-11/realworld.py
Normal file
95
wxPython/samples/wxPIA_book/Chapter-11/realworld.py
Normal file
@@ -0,0 +1,95 @@
|
||||
import wx
|
||||
|
||||
class TestFrame(wx.Frame):
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, -1, "Real World Test")
|
||||
panel = wx.Panel(self)
|
||||
|
||||
# First create the controls
|
||||
topLbl = wx.StaticText(panel, -1, "Account Information")
|
||||
topLbl.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))
|
||||
|
||||
nameLbl = wx.StaticText(panel, -1, "Name:")
|
||||
name = wx.TextCtrl(panel, -1, "");
|
||||
|
||||
addrLbl = wx.StaticText(panel, -1, "Address:")
|
||||
addr1 = wx.TextCtrl(panel, -1, "");
|
||||
addr2 = wx.TextCtrl(panel, -1, "");
|
||||
|
||||
cstLbl = wx.StaticText(panel, -1, "City, State, Zip:")
|
||||
city = wx.TextCtrl(panel, -1, "", size=(150,-1));
|
||||
state = wx.TextCtrl(panel, -1, "", size=(50,-1));
|
||||
zip = wx.TextCtrl(panel, -1, "", size=(70,-1));
|
||||
|
||||
phoneLbl = wx.StaticText(panel, -1, "Phone:")
|
||||
phone = wx.TextCtrl(panel, -1, "");
|
||||
|
||||
emailLbl = wx.StaticText(panel, -1, "Email:")
|
||||
email = wx.TextCtrl(panel, -1, "");
|
||||
|
||||
saveBtn = wx.Button(panel, -1, "Save")
|
||||
cancelBtn = wx.Button(panel, -1, "Cancel")
|
||||
|
||||
# Now do the layout.
|
||||
|
||||
# mainSizer is the top-level one that manages everything
|
||||
mainSizer = wx.BoxSizer(wx.VERTICAL)
|
||||
mainSizer.Add(topLbl, 0, wx.ALL, 5)
|
||||
mainSizer.Add(wx.StaticLine(panel), 0,
|
||||
wx.EXPAND|wx.TOP|wx.BOTTOM, 5)
|
||||
|
||||
# addrSizer is a grid that holds all of the address info
|
||||
addrSizer = wx.FlexGridSizer(cols=2, hgap=5, vgap=5)
|
||||
addrSizer.AddGrowableCol(1)
|
||||
addrSizer.Add(nameLbl, 0,
|
||||
wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
|
||||
addrSizer.Add(name, 0, wx.EXPAND)
|
||||
addrSizer.Add(addrLbl, 0,
|
||||
wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
|
||||
addrSizer.Add(addr1, 0, wx.EXPAND)
|
||||
addrSizer.Add((10,10)) # some empty space
|
||||
addrSizer.Add(addr2, 0, wx.EXPAND)
|
||||
|
||||
addrSizer.Add(cstLbl, 0,
|
||||
wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
|
||||
|
||||
# the city, state, zip fields are in a sub-sizer
|
||||
cstSizer = wx.BoxSizer(wx.HORIZONTAL)
|
||||
cstSizer.Add(city, 1)
|
||||
cstSizer.Add(state, 0, wx.LEFT|wx.RIGHT, 5)
|
||||
cstSizer.Add(zip)
|
||||
addrSizer.Add(cstSizer, 0, wx.EXPAND)
|
||||
|
||||
addrSizer.Add(phoneLbl, 0,
|
||||
wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
|
||||
addrSizer.Add(phone, 0, wx.EXPAND)
|
||||
addrSizer.Add(emailLbl, 0,
|
||||
wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
|
||||
addrSizer.Add(email, 0, wx.EXPAND)
|
||||
|
||||
# now add the addrSizer to the mainSizer
|
||||
mainSizer.Add(addrSizer, 0, wx.EXPAND|wx.ALL, 10)
|
||||
|
||||
# The buttons sizer will put them in a row with resizeable
|
||||
# gaps between and on either side of the buttons
|
||||
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
|
||||
btnSizer.Add((20,20), 1)
|
||||
btnSizer.Add(saveBtn)
|
||||
btnSizer.Add((20,20), 1)
|
||||
btnSizer.Add(cancelBtn)
|
||||
btnSizer.Add((20,20), 1)
|
||||
|
||||
mainSizer.Add(btnSizer, 0, wx.EXPAND|wx.BOTTOM, 10)
|
||||
|
||||
panel.SetSizer(mainSizer)
|
||||
|
||||
# Fit the frame to the needs of the sizer. The frame will
|
||||
# automatically resize the panel as needed. Also prevent the
|
||||
# frame from getting smaller than this size.
|
||||
mainSizer.Fit(self)
|
||||
mainSizer.SetSizeHints(self)
|
||||
|
||||
|
||||
app = wx.PySimpleApp()
|
||||
TestFrame().Show()
|
||||
app.MainLoop()
|
@@ -0,0 +1,26 @@
|
||||
import wx
|
||||
from blockwindow import BlockWindow
|
||||
|
||||
labels = "one two three four five six seven eight nine".split()
|
||||
|
||||
class TestFrame(wx.Frame):
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, -1, "Resizing Flex Grid Sizer")
|
||||
sizer = wx.FlexGridSizer(rows=3, cols=3, hgap=5, vgap=5)
|
||||
for label in labels:
|
||||
bw = BlockWindow(self, label=label)
|
||||
sizer.Add(bw, 0, 0)
|
||||
center = self.FindWindowByName("five")
|
||||
center.SetMinSize((150,50))
|
||||
sizer.AddGrowableCol(0, 1)
|
||||
sizer.AddGrowableCol(1, 2)
|
||||
sizer.AddGrowableCol(2, 1)
|
||||
sizer.AddGrowableRow(0, 1)
|
||||
sizer.AddGrowableRow(1, 5)
|
||||
sizer.AddGrowableRow(2, 1)
|
||||
self.SetSizer(sizer)
|
||||
self.Fit()
|
||||
|
||||
app = wx.PySimpleApp()
|
||||
TestFrame().Show()
|
||||
app.MainLoop()
|
22
wxPython/samples/wxPIA_book/Chapter-11/resizegridsizer.py
Normal file
22
wxPython/samples/wxPIA_book/Chapter-11/resizegridsizer.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import wx
|
||||
from blockwindow import BlockWindow
|
||||
|
||||
labels = "one two three four five six seven eight nine".split()
|
||||
flags = {"one": wx.ALIGN_BOTTOM, "two": wx.ALIGN_CENTER,
|
||||
"four": wx.ALIGN_RIGHT, "six": wx.EXPAND, "seven": wx.EXPAND,
|
||||
"eight": wx.SHAPED}
|
||||
|
||||
class TestFrame(wx.Frame):
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, -1, "GridSizer Resizing")
|
||||
sizer = wx.GridSizer(rows=3, cols=3, hgap=5, vgap=5)
|
||||
for label in labels:
|
||||
bw = BlockWindow(self, label=label)
|
||||
flag = flags.get(label, 0)
|
||||
sizer.Add(bw, 0, flag)
|
||||
self.SetSizer(sizer)
|
||||
self.Fit()
|
||||
|
||||
app = wx.PySimpleApp()
|
||||
TestFrame().Show()
|
||||
app.MainLoop()
|
45
wxPython/samples/wxPIA_book/Chapter-11/staticboxsizer.py
Normal file
45
wxPython/samples/wxPIA_book/Chapter-11/staticboxsizer.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import wx
|
||||
from blockwindow import BlockWindow
|
||||
|
||||
labels = "one two three four five six seven eight nine".split()
|
||||
|
||||
class TestFrame(wx.Frame):
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, -1, "StaticBoxSizer Test")
|
||||
self.panel = wx.Panel(self)
|
||||
|
||||
# make three static boxes with windows positioned inside them
|
||||
box1 = self.MakeStaticBoxSizer("Box 1", labels[0:3])
|
||||
box2 = self.MakeStaticBoxSizer("Box 2", labels[3:6])
|
||||
box3 = self.MakeStaticBoxSizer("Box 3", labels[6:9])
|
||||
|
||||
# We can also use a sizer to manage the placement of other
|
||||
# sizers (and therefore the windows and sub-sizers that they
|
||||
# manage as well.)
|
||||
sizer = wx.BoxSizer(wx.HORIZONTAL)
|
||||
sizer.Add(box1, 0, wx.ALL, 10)
|
||||
sizer.Add(box2, 0, wx.ALL, 10)
|
||||
sizer.Add(box3, 0, wx.ALL, 10)
|
||||
|
||||
self.panel.SetSizer(sizer)
|
||||
sizer.Fit(self)
|
||||
|
||||
|
||||
def MakeStaticBoxSizer(self, boxlabel, itemlabels):
|
||||
# first the static box
|
||||
box = wx.StaticBox(self.panel, -1, boxlabel)
|
||||
|
||||
# then the sizer
|
||||
sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
|
||||
|
||||
# then add items to it like normal
|
||||
for label in itemlabels:
|
||||
bw = BlockWindow(self.panel, label=label)
|
||||
sizer.Add(bw, 0, wx.ALL, 2)
|
||||
|
||||
return sizer
|
||||
|
||||
|
||||
app = wx.PySimpleApp()
|
||||
TestFrame().Show()
|
||||
app.MainLoop()
|
Reference in New Issue
Block a user