0
# Layout Management
1
2
Automatic positioning and sizing of widgets using layout managers including grid layouts for complex arrangements and box layouts for linear organization.
3
4
## Capabilities
5
6
### Base Layout Class
7
8
TTkLayout is the abstract base class for all layout managers, providing common functionality for widget arrangement.
9
10
```python { .api }
11
class TTkLayout:
12
def __init__(self):
13
"""Initialize the base layout."""
14
15
def addWidget(self, widget):
16
"""Add a widget to the layout."""
17
18
def removeWidget(self, widget):
19
"""Remove a widget from the layout."""
20
21
def replaceWidget(self, oldWidget, newWidget):
22
"""Replace one widget with another."""
23
24
def addItem(self, item):
25
"""Add a layout item to the layout."""
26
27
def itemAt(self, index):
28
"""Get the layout item at the specified index."""
29
30
def count(self):
31
"""Get the number of items in the layout."""
32
33
def setGeometry(self, x, y, width, height):
34
"""Set the geometry of the layout area."""
35
36
def geometry(self):
37
"""Get the current geometry of the layout area."""
38
39
def update(self):
40
"""Update the layout, repositioning all widgets."""
41
```
42
43
### Layout Item Wrapper
44
45
TTkLayoutItem wraps widgets and other items for use in layouts.
46
47
```python { .api }
48
class TTkLayoutItem:
49
def __init__(self, widget=None, layout=None):
50
"""
51
Initialize a layout item.
52
53
Parameters:
54
- widget (TTkWidget): Widget to wrap in this item
55
- layout (TTkLayout): Sub-layout to wrap in this item
56
"""
57
58
def widget(self):
59
"""Get the widget wrapped by this item."""
60
61
def layout(self):
62
"""Get the layout wrapped by this item."""
63
64
def setGeometry(self, x, y, width, height):
65
"""Set the geometry for this item."""
66
67
def geometry(self):
68
"""Get the current geometry of this item."""
69
70
def minimumSize(self):
71
"""Get the minimum size for this item."""
72
73
def maximumSize(self):
74
"""Get the maximum size for this item."""
75
```
76
77
### Grid Layout
78
79
TTkGridLayout arranges widgets in a grid pattern with rows and columns, allowing for complex UI arrangements.
80
81
```python { .api }
82
class TTkGridLayout(TTkLayout):
83
def __init__(self):
84
"""Initialize a grid layout."""
85
86
def addWidget(self, widget, row, col, rowspan=1, colspan=1, alignment=None):
87
"""
88
Add a widget to the grid layout.
89
90
Parameters:
91
- widget (TTkWidget): Widget to add
92
- row (int): Grid row position (0-based)
93
- col (int): Grid column position (0-based)
94
- rowspan (int): Number of rows to span (default: 1)
95
- colspan (int): Number of columns to span (default: 1)
96
- alignment (int): Alignment within the cell
97
"""
98
99
def addLayout(self, layout, row, col, rowspan=1, colspan=1, alignment=None):
100
"""
101
Add a sub-layout to the grid layout.
102
103
Parameters:
104
- layout (TTkLayout): Layout to add
105
- row (int): Grid row position
106
- col (int): Grid column position
107
- rowspan (int): Number of rows to span
108
- colspan (int): Number of columns to span
109
- alignment (int): Alignment within the cell
110
"""
111
112
def setRowMinimumHeight(self, row, height):
113
"""Set minimum height for a specific row."""
114
115
def setColumnMinimumWidth(self, col, width):
116
"""Set minimum width for a specific column."""
117
118
def setRowStretch(self, row, stretch):
119
"""Set stretch factor for a row."""
120
121
def setColumnStretch(self, col, stretch):
122
"""Set stretch factor for a column."""
123
124
def rowMinimumHeight(self, row):
125
"""Get minimum height for a row."""
126
127
def columnMinimumWidth(self, col):
128
"""Get minimum width for a column."""
129
130
def rowStretch(self, row):
131
"""Get stretch factor for a row."""
132
133
def columnStretch(self, col):
134
"""Get stretch factor for a column."""
135
136
def rowCount(self):
137
"""Get the number of rows in the grid."""
138
139
def columnCount(self):
140
"""Get the number of columns in the grid."""
141
142
def setHorizontalSpacing(self, spacing):
143
"""Set horizontal spacing between columns."""
144
145
def setVerticalSpacing(self, spacing):
146
"""Set vertical spacing between rows."""
147
148
def horizontalSpacing(self):
149
"""Get horizontal spacing between columns."""
150
151
def verticalSpacing(self):
152
"""Get vertical spacing between rows."""
153
```
154
155
### Vertical Box Layout
156
157
TTkVBoxLayout arranges widgets vertically in a single column.
158
159
```python { .api }
160
class TTkVBoxLayout(TTkLayout):
161
def __init__(self):
162
"""Initialize a vertical box layout."""
163
164
def addWidget(self, widget, stretch=0, alignment=None):
165
"""
166
Add a widget to the vertical layout.
167
168
Parameters:
169
- widget (TTkWidget): Widget to add
170
- stretch (int): Stretch factor for this widget (default: 0)
171
- alignment (int): Alignment of the widget
172
"""
173
174
def addLayout(self, layout, stretch=0):
175
"""
176
Add a sub-layout to the vertical layout.
177
178
Parameters:
179
- layout (TTkLayout): Layout to add
180
- stretch (int): Stretch factor for this layout
181
"""
182
183
def addStretch(self, stretch=0):
184
"""
185
Add stretchable space to the layout.
186
187
Parameters:
188
- stretch (int): Stretch factor for the space
189
"""
190
191
def addSpacing(self, size):
192
"""
193
Add fixed spacing to the layout.
194
195
Parameters:
196
- size (int): Size of the spacing in characters
197
"""
198
199
def insertWidget(self, index, widget, stretch=0, alignment=None):
200
"""Insert a widget at a specific position."""
201
202
def insertLayout(self, index, layout, stretch=0):
203
"""Insert a layout at a specific position."""
204
205
def insertStretch(self, index, stretch=0):
206
"""Insert stretchable space at a specific position."""
207
208
def insertSpacing(self, index, size):
209
"""Insert fixed spacing at a specific position."""
210
211
def setSpacing(self, spacing):
212
"""Set spacing between items."""
213
214
def spacing(self):
215
"""Get spacing between items."""
216
217
def setContentsMargins(self, left, top, right, bottom):
218
"""Set margins around the layout content."""
219
```
220
221
### Horizontal Box Layout
222
223
TTkHBoxLayout arranges widgets horizontally in a single row.
224
225
```python { .api }
226
class TTkHBoxLayout(TTkLayout):
227
def __init__(self):
228
"""Initialize a horizontal box layout."""
229
230
def addWidget(self, widget, stretch=0, alignment=None):
231
"""
232
Add a widget to the horizontal layout.
233
234
Parameters:
235
- widget (TTkWidget): Widget to add
236
- stretch (int): Stretch factor for this widget (default: 0)
237
- alignment (int): Alignment of the widget
238
"""
239
240
def addLayout(self, layout, stretch=0):
241
"""
242
Add a sub-layout to the horizontal layout.
243
244
Parameters:
245
- layout (TTkLayout): Layout to add
246
- stretch (int): Stretch factor for this layout
247
"""
248
249
def addStretch(self, stretch=0):
250
"""
251
Add stretchable space to the layout.
252
253
Parameters:
254
- stretch (int): Stretch factor for the space
255
"""
256
257
def addSpacing(self, size):
258
"""
259
Add fixed spacing to the layout.
260
261
Parameters:
262
- size (int): Size of the spacing in characters
263
"""
264
265
def insertWidget(self, index, widget, stretch=0, alignment=None):
266
"""Insert a widget at a specific position."""
267
268
def insertLayout(self, index, layout, stretch=0):
269
"""Insert a layout at a specific position."""
270
271
def insertStretch(self, index, stretch=0):
272
"""Insert stretchable space at a specific position."""
273
274
def insertSpacing(self, index, size):
275
"""Insert fixed spacing at a specific position."""
276
277
def setSpacing(self, spacing):
278
"""Set spacing between items."""
279
280
def spacing(self):
281
"""Get spacing between items."""
282
283
def setContentsMargins(self, left, top, right, bottom):
284
"""Set margins around the layout content."""
285
```
286
287
## Usage Examples
288
289
### Grid Layout Example
290
291
```python
292
import TermTk as ttk
293
294
root = ttk.TTk()
295
container = ttk.TTkContainer(parent=root)
296
297
# Create a grid layout
298
layout = ttk.TTkGridLayout()
299
300
# Add widgets to the grid
301
button1 = ttk.TTkButton(text="Button 1")
302
button2 = ttk.TTkButton(text="Button 2")
303
button3 = ttk.TTkButton(text="Button 3")
304
label = ttk.TTkLabel(text="Status: Ready")
305
306
# Position widgets in grid (row, col, rowspan, colspan)
307
layout.addWidget(button1, 0, 0) # Row 0, Col 0
308
layout.addWidget(button2, 0, 1) # Row 0, Col 1
309
layout.addWidget(button3, 1, 0, 1, 2) # Row 1, spans 2 columns
310
layout.addWidget(label, 2, 0, 1, 2) # Row 2, spans 2 columns
311
312
# Configure grid properties
313
layout.setColumnStretch(0, 1) # Column 0 stretches
314
layout.setColumnStretch(1, 1) # Column 1 stretches
315
layout.setRowMinimumHeight(0, 3) # Minimum height for row 0
316
317
# Set the layout on the container
318
container.setLayout(layout)
319
320
root.mainloop()
321
```
322
323
### Vertical Box Layout Example
324
325
```python
326
import TermTk as ttk
327
328
root = ttk.TTk()
329
container = ttk.TTkContainer(parent=root)
330
331
# Create a vertical box layout
332
layout = ttk.TTkVBoxLayout()
333
334
# Add widgets vertically
335
title = ttk.TTkLabel(text="Application Title")
336
button1 = ttk.TTkButton(text="Action 1")
337
button2 = ttk.TTkButton(text="Action 2")
338
status = ttk.TTkLabel(text="Ready")
339
340
layout.addWidget(title)
341
layout.addStretch(1) # Add stretchy space
342
layout.addWidget(button1)
343
layout.addSpacing(2) # Add fixed spacing
344
layout.addWidget(button2)
345
layout.addStretch(1) # Add stretchy space
346
layout.addWidget(status)
347
348
# Set spacing between items
349
layout.setSpacing(1)
350
351
container.setLayout(layout)
352
root.mainloop()
353
```
354
355
### Horizontal Box Layout Example
356
357
```python
358
import TermTk as ttk
359
360
root = ttk.TTk()
361
container = ttk.TTkContainer(parent=root)
362
363
# Create horizontal layout
364
layout = ttk.TTkHBoxLayout()
365
366
# Add widgets horizontally
367
label = ttk.TTkLabel(text="Name:")
368
edit = ttk.TTkLineEdit()
369
button = ttk.TTkButton(text="Submit")
370
371
layout.addWidget(label)
372
layout.addWidget(edit, stretch=1) # Edit field stretches
373
layout.addWidget(button)
374
375
container.setLayout(layout)
376
root.mainloop()
377
```
378
379
### Nested Layouts Example
380
381
```python
382
import TermTk as ttk
383
384
root = ttk.TTk()
385
container = ttk.TTkContainer(parent=root)
386
387
# Main vertical layout
388
main_layout = ttk.TTkVBoxLayout()
389
390
# Header section
391
header = ttk.TTkLabel(text="Settings Panel")
392
main_layout.addWidget(header)
393
394
# Middle section with horizontal layout
395
middle_layout = ttk.TTkHBoxLayout()
396
label = ttk.TTkLabel(text="Option:")
397
checkbox = ttk.TTkCheckbox(text="Enable feature")
398
middle_layout.addWidget(label)
399
middle_layout.addWidget(checkbox)
400
middle_layout.addStretch(1)
401
402
main_layout.addLayout(middle_layout)
403
404
# Bottom section with button grid
405
button_layout = ttk.TTkGridLayout()
406
ok_btn = ttk.TTkButton(text="OK")
407
cancel_btn = ttk.TTkButton(text="Cancel")
408
apply_btn = ttk.TTkButton(text="Apply")
409
410
button_layout.addWidget(ok_btn, 0, 0)
411
button_layout.addWidget(cancel_btn, 0, 1)
412
button_layout.addWidget(apply_btn, 0, 2)
413
414
main_layout.addLayout(button_layout)
415
416
container.setLayout(main_layout)
417
root.mainloop()
418
```