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
44 | wx
.FULL_REPAINT_ON_RESIZE
,
48 self
.mainPanel
.SetAutoLayout(True)
50 self
.okButton
= wx
.Button(
51 label
='OK', id=ID_ANCHORSDEMOFRAMEOKBUTTON
,
52 parent
=self
.mainPanel
, name
='okButton',
53 size
=(72, 24), style
=0, pos
=(240, 128)
56 self
.okButton
.SetConstraints(
57 anchors
.LayoutAnchors(self
.okButton
, False, False, True, True)
61 wx
.EVT_BUTTON
, self
.OnOkButtonButton
, id=ID_ANCHORSDEMOFRAMEOKBUTTON
64 self
.backgroundPanel
= wx
.Panel(
65 size
=(304, 80), parent
=self
.mainPanel
,
66 id=ID_ANCHORSDEMOFRAMEBACKGROUNDPANEL
,
67 name
='backgroundPanel',
68 style
=wx
.SIMPLE_BORDER | wx
.CLIP_CHILDREN
,
72 self
.backgroundPanel
.SetBackgroundColour(wx
.Colour(255, 255, 255))
73 self
.backgroundPanel
.SetConstraints(
74 anchors
.LayoutAnchors(self
.backgroundPanel
, True, True, True, True)
77 self
.anchoredPanel
= wx
.Panel(
78 size
=(88, 48), id=ID_ANCHORSDEMOFRAMEANCHOREDPANEL
,
79 parent
=self
.backgroundPanel
, name
='anchoredPanel',
80 style
=wx
.SIMPLE_BORDER
, pos
=(104, 16)
83 self
.anchoredPanel
.SetBackgroundColour(wx
.Colour(0, 0, 222))
84 self
.anchoredPanel
.SetConstraints(
85 anchors
.LayoutAnchors(self
.anchoredPanel
, False, False, False, False)
88 self
.leftCheckBox
= wx
.CheckBox(
89 label
='Left', id=ID_ANCHORSDEMOFRAMELEFTCHECKBOX
,
90 parent
=self
.mainPanel
, name
='leftCheckBox',
91 size
=(40, 16), style
=0, pos
=(8, 8)
94 self
.leftCheckBox
.SetConstraints(
95 anchors
.LayoutAnchors(self
.leftCheckBox
, False, True, False, False)
99 wx
.EVT_CHECKBOX
, self
.OnCheckboxCheckbox
, source
=self
.leftCheckBox
,
100 id=ID_ANCHORSDEMOFRAMELEFTCHECKBOX
103 self
.topCheckBox
= wx
.CheckBox(
104 label
='Top', id=ID_ANCHORSDEMOFRAMETOPCHECKBOX
,
105 parent
=self
.mainPanel
, name
='topCheckBox',
106 size
=(40, 16), style
=0, pos
=(88, 8)
109 self
.topCheckBox
.SetConstraints(
110 anchors
.LayoutAnchors(self
.topCheckBox
, False, True, False, False)
114 wx
.EVT_CHECKBOX
, self
.OnCheckboxCheckbox
, source
=self
.topCheckBox
,
115 id=ID_ANCHORSDEMOFRAMETOPCHECKBOX
118 self
.rightCheckBox
= wx
.CheckBox(
119 label
='Right', id=ID_ANCHORSDEMOFRAMERIGHTCHECKBOX
,
120 parent
=self
.mainPanel
, name
='rightCheckBox',
121 size
=(48, 16), style
=0, pos
=(168, 8)
124 self
.rightCheckBox
.SetConstraints(
125 anchors
.LayoutAnchors(self
.rightCheckBox
, False, True, False, False)
129 wx
.EVT_CHECKBOX
, self
.OnCheckboxCheckbox
, source
=self
.rightCheckBox
,
130 id=ID_ANCHORSDEMOFRAMERIGHTCHECKBOX
133 self
.bottomCheckBox
= wx
.CheckBox(
134 label
='Bottom', id=ID_ANCHORSDEMOFRAMEBOTTOMCHECKBOX
,
135 parent
=self
.mainPanel
, name
='bottomCheckBox',
136 size
=(56, 16), style
=0, pos
=(248, 8)
139 self
.bottomCheckBox
.SetConstraints(
140 anchors
.LayoutAnchors(self
.bottomCheckBox
, False, True, False, False)
144 wx
.EVT_CHECKBOX
, self
.OnCheckboxCheckbox
, source
=self
.bottomCheckBox
,
145 id=ID_ANCHORSDEMOFRAMEBOTTOMCHECKBOX
148 self
.helpStaticText
= wx
.StaticText(
149 label
='Select anchor options above, then resize window to see the effect',
150 id=ID_ANCHORSDEMOFRAMEHELPSTATICTEXT
,
151 parent
=self
.mainPanel
, name
='helpStaticText',
152 size
=(224, 24), style
=wx
.ST_NO_AUTORESIZE
,
156 self
.helpStaticText
.SetConstraints(
157 anchors
.LayoutAnchors(self
.helpStaticText
, True, False, True, True)
160 def __init__(self
, parent
):
161 self
._init
_ctrls
(parent
)
163 # Based on the values of the above checkboxes, we will adjust the layout constraints
164 # on the sample window whenever one of the checkboxes changes state.
165 def OnCheckboxCheckbox(self
, event
):
166 self
.anchoredPanel
.SetConstraints(
167 anchors
.LayoutAnchors(self
.anchoredPanel
,
168 self
.leftCheckBox
.GetValue(), self
.topCheckBox
.GetValue(),
169 self
.rightCheckBox
.GetValue(), self
.bottomCheckBox
.GetValue()
173 def OnOkButtonButton(self
, event
):
176 #---------------------------------------------------------------------------
178 class TestPanel(wx
.Panel
):
179 def __init__(self
, parent
, log
):
181 wx
.Panel
.__init
__(self
, parent
, -1)
183 b
= wx
.Button(self
, -1, "Show the LayoutAnchors sample", (50,50))
184 self
.Bind(wx
.EVT_BUTTON
, self
.OnButton
, b
)
187 def OnButton(self
, evt
):
188 win
= AnchorsDemoFrame(self
)
192 #---------------------------------------------------------------------------
195 def runTest(frame
, nb
, log
):
196 win
= TestPanel(nb
, log
)
199 #----------------------------------------------------------------------
202 overview
= """<html><body>
203 <h2>LayoutAnchors</h2>
204 A class that implements Delphi's Anchors with wxLayoutConstraints.
206 Anchored sides maintain the distance from the edge of the
207 control to the same edge of the parent.
208 When neither side is selected, the control keeps the same
209 relative position to both sides.
211 The current position and size of the control and it's parent
212 is used when setting up the constraints. To change the size or
213 position of an already anchored control, set the constraints to
214 None, reposition or resize and reapply the anchors.
218 Let's anchor the right and bottom edge of a control and
222 ctrl.SetConstraints(LayoutAnchors(ctrl, left=0, top=0, right=1, bottom=1))
224 +=========+ +===================+
227 | +--*--+ | | +-----+ |
230 +-------------------+
234 When anchored on both sides the control will stretch horizontally.
237 ctrl.SetConstraints(LayoutAnchors(ctrl, 1, 0, 1, 1))
239 +=========+ +===================+
242 | +--*--+ | | +---------------+ |
243 +---------+ | * ctrl * |
244 | +-------*-------+ |
245 +-------------------+
253 if __name__
== '__main__':
256 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])