1 # 11/6/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
4 # o In the lambda function at the top, removed the leading 'wx' from the
5 # ID names to remove confusion with 'official' wx members.
9 import wx
.lib
.anchors
as anchors
11 #----------------------------------------------------------------------
13 # Nifty little trick here; apply wx.NewId() to generate a series of
14 # IDs used later on in the app.
16 [ ID_ANCHORSDEMOFRAMEANCHOREDPANEL
,
17 ID_ANCHORSDEMOFRAMEHELPSTATICTEXT
,
18 ID_ANCHORSDEMOFRAMEMAINPANEL
,
19 ID_ANCHORSDEMOFRAMEBACKGROUNDPANEL
,
20 ID_ANCHORSDEMOFRAMERIGHTCHECKBOX
,
21 ID_ANCHORSDEMOFRAMEOKBUTTON
,
22 ID_ANCHORSDEMOFRAMETOPCHECKBOX
,
23 ID_ANCHORSDEMOFRAMEBOTTOMCHECKBOX
,
25 ID_ANCHORSDEMOFRAMELEFTCHECKBOX
,
26 ] = map(lambda _init_ctrls
: wx
.NewId(), range(10))
28 # A small note here: while only certain parts of this frame are actually demonstrating
29 # the capabilities of the LayoutAnchors feature, all the controls are within the same
30 # frame; therefore, all controls and windows within the frame must use LayoutAnchors.
31 # You can't mix LayoutAnchors and sizers.
32 class AnchorsDemoFrame(wx
.Frame
):
33 def _init_utils(self
):
36 def _init_ctrls(self
, prnt
):
38 self
, size
=(328, 187), id=ID_ANCHORSDEMOFRAME
,
39 title
='LayoutAnchors Demonstration', parent
=prnt
,
40 name
='AnchorsDemoFrame',
41 style
= wx
.DEFAULT_FRAME_STYLE | wx
.CLIP_CHILDREN
, pos
=(261, 123)
46 self
.mainPanel
= wx
.Panel(
47 size
=(320, 160), parent
=self
,
48 id=ID_ANCHORSDEMOFRAMEMAINPANEL
, name
='panel1',
49 style
=wx
.TAB_TRAVERSAL | wx
.CLIP_CHILDREN
,
53 self
.mainPanel
.SetAutoLayout(True)
55 self
.okButton
= wx
.Button(
56 label
='OK', id=ID_ANCHORSDEMOFRAMEOKBUTTON
,
57 parent
=self
.mainPanel
, name
='okButton',
58 size
=(72, 24), style
=0, pos
=(240, 128)
61 self
.okButton
.SetConstraints(
62 anchors
.LayoutAnchors(self
.okButton
, False, False, True, True)
66 wx
.EVT_BUTTON
, self
.OnOkButtonButton
, id=ID_ANCHORSDEMOFRAMEOKBUTTON
69 self
.backgroundPanel
= wx
.Panel(
70 size
=(304, 80), parent
=self
.mainPanel
,
71 id=ID_ANCHORSDEMOFRAMEBACKGROUNDPANEL
,
72 name
='backgroundPanel',
73 style
=wx
.SIMPLE_BORDER | wx
.CLIP_CHILDREN
,
77 self
.backgroundPanel
.SetBackgroundColour(wx
.Colour(255, 255, 255))
78 self
.backgroundPanel
.SetConstraints(
79 anchors
.LayoutAnchors(self
.backgroundPanel
, True, True, True, True)
82 self
.anchoredPanel
= wx
.Panel(
83 size
=(88, 48), id=ID_ANCHORSDEMOFRAMEANCHOREDPANEL
,
84 parent
=self
.backgroundPanel
, name
='anchoredPanel',
85 style
=wx
.SIMPLE_BORDER
, pos
=(104, 16)
88 self
.anchoredPanel
.SetBackgroundColour(wx
.Colour(0, 0, 222))
89 self
.anchoredPanel
.SetConstraints(
90 anchors
.LayoutAnchors(self
.anchoredPanel
, False, False, False, False)
93 self
.leftCheckBox
= wx
.CheckBox(
94 label
='Left', id=ID_ANCHORSDEMOFRAMELEFTCHECKBOX
,
95 parent
=self
.mainPanel
, name
='leftCheckBox',
96 size
=(40, 16), style
=0, pos
=(8, 8)
99 self
.leftCheckBox
.SetConstraints(
100 anchors
.LayoutAnchors(self
.leftCheckBox
, False, True, False, False)
104 wx
.EVT_CHECKBOX
, self
.OnCheckboxCheckbox
, source
=self
.leftCheckBox
,
105 id=ID_ANCHORSDEMOFRAMELEFTCHECKBOX
108 self
.topCheckBox
= wx
.CheckBox(
109 label
='Top', id=ID_ANCHORSDEMOFRAMETOPCHECKBOX
,
110 parent
=self
.mainPanel
, name
='topCheckBox',
111 size
=(40, 16), style
=0, pos
=(88, 8)
114 self
.topCheckBox
.SetConstraints(
115 anchors
.LayoutAnchors(self
.topCheckBox
, False, True, False, False)
119 wx
.EVT_CHECKBOX
, self
.OnCheckboxCheckbox
, source
=self
.topCheckBox
,
120 id=ID_ANCHORSDEMOFRAMETOPCHECKBOX
123 self
.rightCheckBox
= wx
.CheckBox(
124 label
='Right', id=ID_ANCHORSDEMOFRAMERIGHTCHECKBOX
,
125 parent
=self
.mainPanel
, name
='rightCheckBox',
126 size
=(48, 16), style
=0, pos
=(168, 8)
129 self
.rightCheckBox
.SetConstraints(
130 anchors
.LayoutAnchors(self
.rightCheckBox
, False, True, False, False)
134 wx
.EVT_CHECKBOX
, self
.OnCheckboxCheckbox
, source
=self
.rightCheckBox
,
135 id=ID_ANCHORSDEMOFRAMERIGHTCHECKBOX
138 self
.bottomCheckBox
= wx
.CheckBox(
139 label
='Bottom', id=ID_ANCHORSDEMOFRAMEBOTTOMCHECKBOX
,
140 parent
=self
.mainPanel
, name
='bottomCheckBox',
141 size
=(56, 16), style
=0, pos
=(248, 8)
144 self
.bottomCheckBox
.SetConstraints(
145 anchors
.LayoutAnchors(self
.bottomCheckBox
, False, True, False, False)
149 wx
.EVT_CHECKBOX
, self
.OnCheckboxCheckbox
, source
=self
.bottomCheckBox
,
150 id=ID_ANCHORSDEMOFRAMEBOTTOMCHECKBOX
153 self
.helpStaticText
= wx
.StaticText(
154 label
='Select anchor options above, then resize window to see the effect',
155 id=ID_ANCHORSDEMOFRAMEHELPSTATICTEXT
,
156 parent
=self
.mainPanel
, name
='helpStaticText',
157 size
=(224, 24), style
=wx
.ST_NO_AUTORESIZE
,
161 self
.helpStaticText
.SetConstraints(
162 anchors
.LayoutAnchors(self
.helpStaticText
, True, False, True, True)
165 def __init__(self
, parent
):
166 self
._init
_ctrls
(parent
)
168 # Based on the values of the above checkboxes, we will adjust the layout constraints
169 # on the sample window whenever one of the checkboxes changes state.
170 def OnCheckboxCheckbox(self
, event
):
171 self
.anchoredPanel
.SetConstraints(
172 anchors
.LayoutAnchors(self
.anchoredPanel
,
173 self
.leftCheckBox
.GetValue(), self
.topCheckBox
.GetValue(),
174 self
.rightCheckBox
.GetValue(), self
.bottomCheckBox
.GetValue()
178 def OnOkButtonButton(self
, event
):
181 #----------------------------------------------------------------------
183 def runTest(frame
, nb
, log
):
184 win
= AnchorsDemoFrame(frame
)
191 #----------------------------------------------------------------------
194 overview
= """<html><body>
195 <h2>LayoutAnchors</h2>
196 A class that implements Delphi's Anchors with wxLayoutConstraints.
198 Anchored sides maintain the distance from the edge of the
199 control to the same edge of the parent.
200 When neither side is selected, the control keeps the same
201 relative position to both sides.
203 The current position and size of the control and it's parent
204 is used when setting up the constraints. To change the size or
205 position of an already anchored control, set the constraints to
206 None, reposition or resize and reapply the anchors.
210 Let's anchor the right and bottom edge of a control and
214 ctrl.SetConstraints(LayoutAnchors(ctrl, left=0, top=0, right=1, bottom=1))
216 +=========+ +===================+
219 | +--*--+ | | +-----+ |
222 +-------------------+
226 When anchored on both sides the control will stretch horizontally.
229 ctrl.SetConstraints(LayoutAnchors(ctrl, 1, 0, 1, 1))
231 +=========+ +===================+
234 | +--*--+ | | +---------------+ |
235 +---------+ | * ctrl * |
236 | +-------*-------+ |
237 +-------------------+
245 if __name__
== '__main__':
248 run
.main(['', os
.path
.basename(sys
.argv
[0])])