3 import wx
.lib
.anchors
as anchors
5 #----------------------------------------------------------------------
7 # Nifty little trick here; apply wx.NewId() to generate a series of
8 # IDs used later on in the app.
10 [ ID_ANCHORSDEMOFRAMEANCHOREDPANEL
,
11 ID_ANCHORSDEMOFRAMEHELPSTATICTEXT
,
12 ID_ANCHORSDEMOFRAMEMAINPANEL
,
13 ID_ANCHORSDEMOFRAMEBACKGROUNDPANEL
,
14 ID_ANCHORSDEMOFRAMERIGHTCHECKBOX
,
15 ID_ANCHORSDEMOFRAMEOKBUTTON
,
16 ID_ANCHORSDEMOFRAMETOPCHECKBOX
,
17 ID_ANCHORSDEMOFRAMEBOTTOMCHECKBOX
,
19 ID_ANCHORSDEMOFRAMELEFTCHECKBOX
,
20 ] = map(lambda _init_ctrls
: wx
.NewId(), range(10))
22 # A small note here: while only certain parts of this frame are actually demonstrating
23 # the capabilities of the LayoutAnchors feature, all the controls are within the same
24 # frame; therefore, all controls and windows within the frame must use LayoutAnchors.
25 # You can't mix LayoutAnchors and sizers.
26 class AnchorsDemoFrame(wx
.Frame
):
27 def _init_utils(self
):
30 def _init_ctrls(self
, prnt
):
32 self
, size
=(328, 187), id=ID_ANCHORSDEMOFRAME
,
33 title
='LayoutAnchors Demonstration', parent
=prnt
,
34 name
='AnchorsDemoFrame',
35 style
= wx
.DEFAULT_FRAME_STYLE | wx
.CLIP_CHILDREN
, pos
=(261, 123)
40 self
.mainPanel
= wx
.Panel(
41 size
=(320, 160), parent
=self
,
42 id=ID_ANCHORSDEMOFRAMEMAINPANEL
, name
='panel1',
43 style
=wx
.TAB_TRAVERSAL | wx
.CLIP_CHILDREN
,
47 self
.mainPanel
.SetAutoLayout(True)
49 self
.okButton
= wx
.Button(
50 label
='OK', id=ID_ANCHORSDEMOFRAMEOKBUTTON
,
51 parent
=self
.mainPanel
, name
='okButton',
52 size
=(72, 24), style
=0, pos
=(240, 128)
55 self
.okButton
.SetConstraints(
56 anchors
.LayoutAnchors(self
.okButton
, False, False, True, True)
60 wx
.EVT_BUTTON
, self
.OnOkButtonButton
, id=ID_ANCHORSDEMOFRAMEOKBUTTON
63 self
.backgroundPanel
= wx
.Panel(
64 size
=(304, 80), parent
=self
.mainPanel
,
65 id=ID_ANCHORSDEMOFRAMEBACKGROUNDPANEL
,
66 name
='backgroundPanel',
67 style
=wx
.SIMPLE_BORDER | wx
.CLIP_CHILDREN
,
71 self
.backgroundPanel
.SetBackgroundColour(wx
.Colour(255, 255, 255))
72 self
.backgroundPanel
.SetConstraints(
73 anchors
.LayoutAnchors(self
.backgroundPanel
, True, True, True, True)
76 self
.anchoredPanel
= wx
.Panel(
77 size
=(88, 48), id=ID_ANCHORSDEMOFRAMEANCHOREDPANEL
,
78 parent
=self
.backgroundPanel
, name
='anchoredPanel',
79 style
=wx
.SIMPLE_BORDER
, pos
=(104, 16)
82 self
.anchoredPanel
.SetBackgroundColour(wx
.Colour(0, 0, 222))
83 self
.anchoredPanel
.SetConstraints(
84 anchors
.LayoutAnchors(self
.anchoredPanel
, False, False, False, False)
87 self
.leftCheckBox
= wx
.CheckBox(
88 label
='Left', id=ID_ANCHORSDEMOFRAMELEFTCHECKBOX
,
89 parent
=self
.mainPanel
, name
='leftCheckBox',
90 size
=(40, 16), style
=0, pos
=(8, 8)
93 self
.leftCheckBox
.SetConstraints(
94 anchors
.LayoutAnchors(self
.leftCheckBox
, False, True, False, False)
98 wx
.EVT_CHECKBOX
, self
.OnCheckboxCheckbox
, source
=self
.leftCheckBox
,
99 id=ID_ANCHORSDEMOFRAMELEFTCHECKBOX
102 self
.topCheckBox
= wx
.CheckBox(
103 label
='Top', id=ID_ANCHORSDEMOFRAMETOPCHECKBOX
,
104 parent
=self
.mainPanel
, name
='topCheckBox',
105 size
=(40, 16), style
=0, pos
=(88, 8)
108 self
.topCheckBox
.SetConstraints(
109 anchors
.LayoutAnchors(self
.topCheckBox
, False, True, False, False)
113 wx
.EVT_CHECKBOX
, self
.OnCheckboxCheckbox
, source
=self
.topCheckBox
,
114 id=ID_ANCHORSDEMOFRAMETOPCHECKBOX
117 self
.rightCheckBox
= wx
.CheckBox(
118 label
='Right', id=ID_ANCHORSDEMOFRAMERIGHTCHECKBOX
,
119 parent
=self
.mainPanel
, name
='rightCheckBox',
120 size
=(48, 16), style
=0, pos
=(168, 8)
123 self
.rightCheckBox
.SetConstraints(
124 anchors
.LayoutAnchors(self
.rightCheckBox
, False, True, False, False)
128 wx
.EVT_CHECKBOX
, self
.OnCheckboxCheckbox
, source
=self
.rightCheckBox
,
129 id=ID_ANCHORSDEMOFRAMERIGHTCHECKBOX
132 self
.bottomCheckBox
= wx
.CheckBox(
133 label
='Bottom', id=ID_ANCHORSDEMOFRAMEBOTTOMCHECKBOX
,
134 parent
=self
.mainPanel
, name
='bottomCheckBox',
135 size
=(56, 16), style
=0, pos
=(248, 8)
138 self
.bottomCheckBox
.SetConstraints(
139 anchors
.LayoutAnchors(self
.bottomCheckBox
, False, True, False, False)
143 wx
.EVT_CHECKBOX
, self
.OnCheckboxCheckbox
, source
=self
.bottomCheckBox
,
144 id=ID_ANCHORSDEMOFRAMEBOTTOMCHECKBOX
147 self
.helpStaticText
= wx
.StaticText(
148 label
='Select anchor options above, then resize window to see the effect',
149 id=ID_ANCHORSDEMOFRAMEHELPSTATICTEXT
,
150 parent
=self
.mainPanel
, name
='helpStaticText',
151 size
=(224, 24), style
=wx
.ST_NO_AUTORESIZE
,
155 self
.helpStaticText
.SetConstraints(
156 anchors
.LayoutAnchors(self
.helpStaticText
, True, False, True, True)
159 def __init__(self
, parent
):
160 self
._init
_ctrls
(parent
)
162 # Based on the values of the above checkboxes, we will adjust the layout constraints
163 # on the sample window whenever one of the checkboxes changes state.
164 def OnCheckboxCheckbox(self
, event
):
165 self
.anchoredPanel
.SetConstraints(
166 anchors
.LayoutAnchors(self
.anchoredPanel
,
167 self
.leftCheckBox
.GetValue(), self
.topCheckBox
.GetValue(),
168 self
.rightCheckBox
.GetValue(), self
.bottomCheckBox
.GetValue()
172 def OnOkButtonButton(self
, event
):
175 #---------------------------------------------------------------------------
177 class TestPanel(wx
.Panel
):
178 def __init__(self
, parent
, log
):
180 wx
.Panel
.__init
__(self
, parent
, -1)
182 b
= wx
.Button(self
, -1, "Show the LayoutAnchors sample", (50,50))
183 self
.Bind(wx
.EVT_BUTTON
, self
.OnButton
, b
)
186 def OnButton(self
, evt
):
187 win
= AnchorsDemoFrame(self
)
191 #---------------------------------------------------------------------------
194 def runTest(frame
, nb
, log
):
195 win
= TestPanel(nb
, log
)
198 #----------------------------------------------------------------------
201 overview
= """<html><body>
202 <h2>LayoutAnchors</h2>
203 A class that implements Delphi's Anchors with wxLayoutConstraints.
205 Anchored sides maintain the distance from the edge of the
206 control to the same edge of the parent.
207 When neither side is selected, the control keeps the same
208 relative position to both sides.
210 The current position and size of the control and it's parent
211 is used when setting up the constraints. To change the size or
212 position of an already anchored control, set the constraints to
213 None, reposition or resize and reapply the anchors.
217 Let's anchor the right and bottom edge of a control and
221 ctrl.SetConstraints(LayoutAnchors(ctrl, left=0, top=0, right=1, bottom=1))
223 +=========+ +===================+
226 | +--*--+ | | +-----+ |
229 +-------------------+
233 When anchored on both sides the control will stretch horizontally.
236 ctrl.SetConstraints(LayoutAnchors(ctrl, 1, 0, 1, 1))
238 +=========+ +===================+
241 | +--*--+ | | +---------------+ |
242 +---------+ | * ctrl * |
243 | +-------*-------+ |
244 +-------------------+
252 if __name__
== '__main__':
255 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])