2 import wxaddons
.sized_controls
as sc
5 <html><body><h2>Sized Controls</h2>
6 SizedControls is an addon library that attempts to simplify the
7 creation of sizer-based layouts. It adds the following classes:
11 This class automatically creates its own sizer (a vertical box sizer
12 by default) and automatically adds its children to the sizer. You can
13 change the SizedPanel's sizer type by calling
14 panel.SetSizerType(\"type\", [args]), where valid types are
15 \"horizontal\", \"vertical\", \"form\" (a 2-col flex grid sizer), and
16 \"grid\". Args include \"cols\" and \"rows\" attributes for
17 grids. This class also applies control borders that adhere to the
18 native platform's Human Interface Guidelines (HIG) on Win, GTK and
21 <h3>SizedFrame and SizedDialog</h3>
23 These classes automatically setup a SizedPanel which is appropriately
24 positioned and given appropriate borders in accordance with the
27 <p>Since controls are added to the parent's sizer upon creation, you
28 don't need to use sizer.Add or even create sizers yourself. You just
29 use SetSizerType() to change the sizer you want to use, and
30 control.SetSizerProps() to change the sizer properties of the
31 control. So as a result, code that used to look like this:
33 <table bgcolor=\"#EFEFEF\"><tr><td><pre>
34 ... wx.Dialog init code...
36 panel = wx.Panel(self, -1)
37 b1 = wx.Button(panel, -1)
38 b2 = wx.Button(panel, -1)
39 t1 = wx.TextCtrl(panel, -1)
40 b3 = wx.Button(panel, -1)
42 sizer = wx.BoxSizer(wx.HORIZONTAL)
43 sizer.Add(b1, 0, wx.ALL, 6)
44 sizer.Add(b2, 0, wx.ALL, 6)
45 sizer.Add(t1, 0, wx.EXPAND | wx.ALL, 6)
46 sizer.Add(b3, 0, wx.ALL, 6)
49 dlgSizer = wx.BoxSizer()
50 dlgSizer.Add(panel, 1, wx.EXPAND)
51 self.SetSizer(dlgSizer)
52 self.SetAutoLayout(True)
54 ... rest of dialog ...</pre>
57 would now look like this:
59 <table bgcolor=\"#EFEFEF\"><tr><td><pre>
60 ... wx.Dialog init code...
62 panel = self.GetContentsPane()
63 panel.SetSizerType(\"horizontal\")
65 b1 = wx.Button(panel, -1)
66 b2 = wx.Button(panel, -1)
68 t1 = wx.TextCtrl(panel, -1)
69 t1.SetSizerProps(expand=True)
71 b3 = wx.Button(panel, -1)
73 ... rest of dialog ...</pre>
76 and the latter example will adhere to HIG spacing guidelines on all platforms,
77 unlike the former example.
79 Please check the demos for more complete and sophisticated examples of SizedControls
83 class FormDialog(sc
.SizedDialog
):
84 def __init__(self
, parent
, id):
85 sc
.SizedDialog
.__init
__(self
, None, -1, "SizedForm Dialog",
86 style
=wx
.DEFAULT_DIALOG_STYLE | wx
.RESIZE_BORDER
)
88 pane
= self
.GetContentsPane()
89 pane
.SetSizerType("form")
92 wx
.StaticText(pane
, -1, "Name")
93 textCtrl
= wx
.TextCtrl(pane
, -1, "Your name here")
94 textCtrl
.SetSizerProps(expand
=True)
97 wx
.StaticText(pane
, -1, "Email")
98 emailCtrl
= wx
.TextCtrl(pane
, -1, "")
99 emailCtrl
.SetSizerProps(expand
=True)
102 wx
.StaticText(pane
, -1, "Gender")
103 wx
.Choice(pane
, -1, choices
=["male", "female"])
106 wx
.StaticText(pane
, -1, "State")
107 wx
.TextCtrl(pane
, -1, size
=(60, -1)) # two chars for state
110 wx
.StaticText(pane
, -1, "Title")
112 # here's how to add a 'nested sizer' using sized_controls
113 radioPane
= sc
.SizedPanel(pane
, -1)
114 radioPane
.SetSizerType("horizontal")
115 radioPane
.SetSizerProps(expand
=True)
117 # make these children of the radioPane to have them use
118 # the horizontal layout
119 wx
.RadioButton(radioPane
, -1, "Mr.")
120 wx
.RadioButton(radioPane
, -1, "Mrs.")
121 wx
.RadioButton(radioPane
, -1, "Dr.")
125 self
.SetButtonSizer(self
.CreateStdDialogButtonSizer(wx
.OK | wx
.CANCEL
))
127 # a little trick to make sure that you can't resize the dialog to
128 # less screen space than the controls need
130 self
.SetMinSize(self
.GetSize())
133 class ErrorDialog(sc
.SizedDialog
):
134 def __init__(self
, parent
, id):
135 sc
.SizedDialog
.__init
__(self
, parent
, id, "Error log viewer",
136 style
=wx
.DEFAULT_DIALOG_STYLE|wx
.RESIZE_BORDER
)
138 # Always use self.GetContentsPane() - this ensures that your dialog
139 # automatically adheres to HIG spacing requirements on all platforms.
140 # pane here is a sc.SizedPanel with a vertical sizer layout. All children
141 # should be added to this pane, NOT to self.
142 pane
= self
.GetContentsPane()
145 self
.listCtrl
= wx
.ListCtrl(pane
, -1, size
=(300, -1), style
=wx
.LC_REPORT
)
146 self
.listCtrl
.SetSizerProps(expand
=True, proportion
=1)
147 self
.ConfigureListCtrl()
150 self
.lblDetails
= wx
.StaticText(pane
, -1, "Error Details")
153 self
.details
= wx
.TextCtrl(pane
, -1, style
=wx
.TE_MULTILINE
)
154 self
.details
.SetSizerProps(expand
=True, proportion
=1)
157 # since we want to use a custom button layout, we won't use the
158 # CreateStdDialogBtnSizer here, we'll just create our own panel with
159 # a horizontal layout and add the buttons to that.
160 btnpane
= sc
.SizedPanel(pane
, -1)
161 btnpane
.SetSizerType("horizontal")
162 btnpane
.SetSizerProps(expand
=True)
164 self
.saveBtn
= wx
.Button(btnpane
, wx
.ID_SAVE
)
165 spacer
= sc
.SizedPanel(btnpane
, -1)
166 spacer
.SetSizerProps(expand
=True, proportion
=1)
168 self
.clearBtn
= wx
.Button(btnpane
, -1, "Clear")
171 self
.SetMinSize(self
.GetSize())
173 def ConfigureListCtrl(self
):
174 self
.listCtrl
.InsertColumn(0, "Time")
175 self
.listCtrl
.InsertColumn(1, "Error Message")
176 self
.listCtrl
.SetColumnWidth(0, 100)
177 self
.listCtrl
.SetColumnWidth(1, 280)
179 #---------------------------------------------------------------------------
181 class TestPanel(wx
.Panel
):
182 def __init__(self
, parent
, log
):
185 wx
.Panel
.__init
__(self
, parent
, -1)
187 b
= wx
.Button(self
, -1, "Sized Controls Form Dialog", (50,50))
188 self
.Bind(wx
.EVT_BUTTON
, self
.OnFormButton
, b
)
190 b2
= wx
.Button(self
, -1, "Sized Controls Error Dialog", (50,90))
191 self
.Bind(wx
.EVT_BUTTON
, self
.OnErrorButton
, b2
)
194 def OnFormButton(self
, evt
):
196 dlg
= FormDialog(self
, -1)
199 # this does not return until the dialog is closed.
200 val
= dlg
.ShowModal()
203 self
.log
.WriteText("You pressed OK\n")
205 self
.log
.WriteText("You pressed Cancel\n")
209 def OnErrorButton(self
, evt
):
211 dlg
= ErrorDialog(self
, -1)
214 # this does not return until the dialog is closed.
215 val
= dlg
.ShowModal()
218 self
.log
.WriteText("You pressed OK\n")
220 self
.log
.WriteText("You pressed Cancel\n")
224 def runTest(frame
, nb
, log
):
225 win
= TestPanel(nb
, log
)
228 if __name__
== "__main__":
229 app
= wx
.PySimpleApp()