0
# Container Widgets
1
2
Specialized container widgets including windows, frames, splitters, and scrollable areas for organizing and structuring complex UIs.
3
4
## Capabilities
5
6
### Window Widget
7
8
TTkWindow provides a top-level container with title bar, borders, and window controls.
9
10
```python { .api }
11
class TTkWindow(TTkContainer):
12
def __init__(self, parent=None, **kwargs):
13
"""
14
Initialize a window widget.
15
16
Parameters:
17
- title (str): Window title
18
- pos (TTkPoint): Window position
19
- size (TTkSize): Window size
20
- flags (int): Window flags for behavior
21
- border (bool): Show window border (default: True)
22
"""
23
24
def setTitle(self, title):
25
"""Set the window title."""
26
27
def title(self):
28
"""Get the window title."""
29
30
def setFlags(self, flags):
31
"""Set window flags."""
32
33
def flags(self):
34
"""Get window flags."""
35
36
def setBorder(self, border):
37
"""Enable/disable window border."""
38
39
def border(self):
40
"""Check if border is enabled."""
41
42
def close(self):
43
"""Close the window."""
44
45
def showMaximized(self):
46
"""Show window maximized."""
47
48
def showNormal(self):
49
"""Show window in normal state."""
50
51
def isMaximized(self):
52
"""Check if window is maximized."""
53
54
def setResizable(self, resizable):
55
"""Enable/disable window resizing."""
56
57
def isResizable(self):
58
"""Check if window is resizable."""
59
60
def setModal(self, modal):
61
"""Set window modality."""
62
63
def isModal(self):
64
"""Check if window is modal."""
65
66
# Signals
67
closed: pyTTkSignal # Emitted when window is closed
68
```
69
70
### Frame Widget
71
72
TTkFrame provides a container with customizable borders and styling.
73
74
```python { .api }
75
class TTkFrame(TTkContainer):
76
def __init__(self, parent=None, **kwargs):
77
"""
78
Initialize a frame widget.
79
80
Parameters:
81
- border (bool): Show frame border
82
- title (str): Frame title
83
- titlePos (int): Title position constant
84
"""
85
86
def setBorder(self, border):
87
"""Enable/disable frame border."""
88
89
def border(self):
90
"""Check if border is enabled."""
91
92
def setTitle(self, title):
93
"""Set the frame title."""
94
95
def title(self):
96
"""Get the frame title."""
97
98
def setTitlePos(self, pos):
99
"""Set title position."""
100
101
def titlePos(self):
102
"""Get title position."""
103
104
def setFrameStyle(self, style):
105
"""Set frame border style."""
106
107
def frameStyle(self):
108
"""Get frame border style."""
109
```
110
111
### Resizable Frame Widget
112
113
TTkResizableFrame extends TTkFrame with user-resizable capabilities.
114
115
```python { .api }
116
class TTkResizableFrame(TTkFrame):
117
def __init__(self, parent=None, **kwargs):
118
"""
119
Initialize a resizable frame widget.
120
121
Parameters:
122
- resizeBorder (int): Resize border width
123
- minSize (TTkSize): Minimum size constraint
124
- maxSize (TTkSize): Maximum size constraint
125
"""
126
127
def setResizeBorder(self, border):
128
"""Set resize border width."""
129
130
def resizeBorder(self):
131
"""Get resize border width."""
132
133
def setMinSize(self, size):
134
"""Set minimum size constraint."""
135
136
def minSize(self):
137
"""Get minimum size constraint."""
138
139
def setMaxSize(self, size):
140
"""Set maximum size constraint."""
141
142
def maxSize(self):
143
"""Get maximum size constraint."""
144
145
def isResizing(self):
146
"""Check if currently being resized."""
147
148
# Signals
149
resized: pyTTkSignal # Emitted when frame is resized
150
```
151
152
### Splitter Widget
153
154
TTkSplitter provides resizable panes that can be adjusted by the user.
155
156
```python { .api }
157
class TTkSplitter(TTkContainer):
158
def __init__(self, parent=None, **kwargs):
159
"""
160
Initialize a splitter widget.
161
162
Parameters:
163
- orientation (int): Horizontal or vertical orientation
164
- sizes (list): Initial sizes for panes
165
"""
166
167
def addWidget(self, widget):
168
"""Add a widget as a new pane."""
169
170
def insertWidget(self, index, widget):
171
"""Insert a widget at specific pane index."""
172
173
def setOrientation(self, orientation):
174
"""Set splitter orientation."""
175
176
def orientation(self):
177
"""Get splitter orientation."""
178
179
def setSizes(self, sizes):
180
"""Set sizes for all panes."""
181
182
def sizes(self):
183
"""Get current sizes of all panes."""
184
185
def setCollapsible(self, index, collapsible):
186
"""Set whether a pane can be collapsed."""
187
188
def isCollapsible(self, index):
189
"""Check if pane can be collapsed."""
190
191
def setStretchFactor(self, index, stretch):
192
"""Set stretch factor for a pane."""
193
194
def stretchFactor(self, index):
195
"""Get stretch factor for a pane."""
196
197
def count(self):
198
"""Get number of panes."""
199
200
def widget(self, index):
201
"""Get widget at pane index."""
202
203
def indexOf(self, widget):
204
"""Get index of widget."""
205
206
def setSplitterHandleSize(self, size):
207
"""Set splitter handle size."""
208
209
def splitterHandleSize(self):
210
"""Get splitter handle size."""
211
212
# Signals
213
splitterMoved: pyTTkSignal # Emitted when splitter is moved
214
```
215
216
### Scroll Area Widget
217
218
TTkScrollArea provides scrollable content area for widgets larger than the visible area.
219
220
```python { .api }
221
class TTkScrollArea(TTkAbstractScrollArea):
222
def __init__(self, parent=None, **kwargs):
223
"""
224
Initialize a scroll area widget.
225
226
Parameters:
227
- widgetResizable (bool): Allow widget to resize with scroll area
228
"""
229
230
def setWidget(self, widget):
231
"""Set the widget to be scrolled."""
232
233
def widget(self):
234
"""Get the scrolled widget."""
235
236
def takeWidget(self):
237
"""Remove and return the scrolled widget."""
238
239
def setWidgetResizable(self, resizable):
240
"""Set whether widget resizes with scroll area."""
241
242
def widgetResizable(self):
243
"""Check if widget resizes with scroll area."""
244
245
def setAlignment(self, alignment):
246
"""Set widget alignment within scroll area."""
247
248
def alignment(self):
249
"""Get widget alignment."""
250
251
def ensureVisible(self, x, y, xmargin=0, ymargin=0):
252
"""Ensure a point is visible in the scroll area."""
253
254
def ensureWidgetVisible(self, childWidget, xmargin=0, ymargin=0):
255
"""Ensure a child widget is visible."""
256
```
257
258
### Tab Widget
259
260
TTkTabWidget provides tabbed container for multiple pages of content.
261
262
```python { .api }
263
class TTkTabWidget(TTkContainer):
264
def __init__(self, parent=None, **kwargs):
265
"""
266
Initialize a tab widget.
267
268
Parameters:
269
- closable (bool): Whether tabs have close buttons (default: False)
270
- barType (TTkBarType): Tab bar type configuration (default: TTkBarType.NONE)
271
"""
272
273
def addTab(self, widget, label, data=None, closable=None):
274
"""Add a tab with widget and label."""
275
276
def insertTab(self, index, widget, label):
277
"""Insert a tab at specific index."""
278
279
def removeTab(self, index):
280
"""Remove tab at index."""
281
282
def setCurrentIndex(self, index):
283
"""Set the current active tab."""
284
285
def currentIndex(self):
286
"""Get the current active tab index."""
287
288
def setCurrentWidget(self, widget):
289
"""Set the current active widget."""
290
291
def currentWidget(self):
292
"""Get the current active widget."""
293
294
def count(self):
295
"""Get number of tabs."""
296
297
def widget(self, index):
298
"""Get widget at tab index."""
299
300
def indexOf(self, widget):
301
"""Get index of widget."""
302
303
def setTabText(self, index, text):
304
"""Set text for tab at index."""
305
306
def tabText(self, index):
307
"""Get text for tab at index."""
308
309
def setTabEnabled(self, index, enabled):
310
"""Enable/disable tab at index."""
311
312
def isTabEnabled(self, index):
313
"""Check if tab is enabled."""
314
315
def setTabVisible(self, index, visible):
316
"""Show/hide tab at index."""
317
318
def isTabVisible(self, index):
319
"""Check if tab is visible."""
320
321
def setTabPosition(self, position):
322
"""Set tab bar position."""
323
324
def tabPosition(self):
325
"""Get tab bar position."""
326
327
def setTabsClosable(self, closable):
328
"""Enable/disable tab close buttons."""
329
330
def tabsClosable(self):
331
"""Check if tabs are closable."""
332
333
def clear(self):
334
"""Remove all tabs."""
335
336
# Signals
337
currentChanged: pyTTkSignal # Emitted when current tab changes
338
tabCloseRequested: pyTTkSignal # Emitted when tab close is requested
339
tabBarClicked: pyTTkSignal # Emitted when tab bar is clicked
340
```
341
342
### Application Template
343
344
TTkAppTemplate provides a common application structure with menu bar, tool bar, status bar, and central widget.
345
346
```python { .api }
347
class TTkAppTemplate(TTkWindow):
348
def __init__(self, parent=None, **kwargs):
349
"""
350
Initialize an application template.
351
352
Parameters:
353
- closable (bool): Whether the application can be closed (default: False)
354
- barType (TTkBarType): Bar type configuration (default: TTkBarType.NONE)
355
"""
356
357
def setCentralWidget(self, widget):
358
"""Set the central widget."""
359
360
def centralWidget(self):
361
"""Get the central widget."""
362
363
def menuBar(self):
364
"""Get the menu bar."""
365
366
def statusBar(self):
367
"""Get the status bar."""
368
369
def addDockWidget(self, area, widget):
370
"""Add a docked widget to specified area."""
371
372
def removeDockWidget(self, widget):
373
"""Remove a docked widget."""
374
375
def setMenuBar(self, menuBar):
376
"""Set custom menu bar."""
377
378
def setStatusBar(self, statusBar):
379
"""Set custom status bar."""
380
```
381
382
## Usage Examples
383
384
### Basic Window Example
385
386
```python
387
import TermTk as ttk
388
389
root = ttk.TTk()
390
391
# Create a window
392
window = ttk.TTkWindow(parent=root,
393
title="My Application",
394
pos=(5, 5),
395
size=(60, 20))
396
397
# Add content to window
398
label = ttk.TTkLabel(parent=window,
399
text="Hello from window!",
400
pos=(10, 5))
401
402
button = ttk.TTkButton(parent=window,
403
text="Close",
404
pos=(10, 8),
405
size=(10, 3))
406
407
# Connect close button
408
@ttk.pyTTkSlot()
409
def close_window():
410
window.close()
411
412
button.clicked.connect(close_window)
413
414
root.mainloop()
415
```
416
417
### Splitter Layout Example
418
419
```python
420
import TermTk as ttk
421
422
root = ttk.TTk()
423
container = ttk.TTkContainer(parent=root)
424
425
# Create horizontal splitter
426
splitter = ttk.TTkSplitter(parent=container,
427
orientation=ttk.TTkConstant.Horizontal)
428
429
# Create panes
430
left_frame = ttk.TTkFrame(title="Left Pane")
431
left_label = ttk.TTkLabel(parent=left_frame, text="Left content")
432
433
right_frame = ttk.TTkFrame(title="Right Pane")
434
right_label = ttk.TTkLabel(parent=right_frame, text="Right content")
435
436
# Add panes to splitter
437
splitter.addWidget(left_frame)
438
splitter.addWidget(right_frame)
439
440
# Set initial sizes (left: 30%, right: 70%)
441
splitter.setSizes([30, 70])
442
443
root.mainloop()
444
```
445
446
### Scrollable Content Example
447
448
```python
449
import TermTk as ttk
450
451
root = ttk.TTk()
452
container = ttk.TTkContainer(parent=root, size=(50, 20))
453
454
# Create scroll area
455
scroll = ttk.TTkScrollArea(parent=container)
456
scroll.setWidgetResizable(True)
457
458
# Create large content widget
459
content = ttk.TTkContainer()
460
layout = ttk.TTkVBoxLayout()
461
462
# Add many widgets to demonstrate scrolling
463
for i in range(50):
464
button = ttk.TTkButton(text=f"Button {i+1}")
465
layout.addWidget(button)
466
467
content.setLayout(layout)
468
scroll.setWidget(content)
469
470
root.mainloop()
471
```
472
473
### Tabbed Interface Example
474
475
```python
476
import TermTk as ttk
477
478
root = ttk.TTk()
479
container = ttk.TTkContainer(parent=root)
480
481
# Create tab widget
482
tabs = ttk.TTkTabWidget(parent=container)
483
484
# Create tab pages
485
page1 = ttk.TTkContainer()
486
page1_layout = ttk.TTkVBoxLayout()
487
page1_layout.addWidget(ttk.TTkLabel(text="This is page 1"))
488
page1_layout.addWidget(ttk.TTkButton(text="Page 1 Button"))
489
page1.setLayout(page1_layout)
490
491
page2 = ttk.TTkContainer()
492
page2_layout = ttk.TTkVBoxLayout()
493
page2_layout.addWidget(ttk.TTkLabel(text="This is page 2"))
494
page2_layout.addWidget(ttk.TTkLineEdit(text="Edit field"))
495
page2.setLayout(page2_layout)
496
497
page3 = ttk.TTkContainer()
498
page3_layout = ttk.TTkVBoxLayout()
499
page3_layout.addWidget(ttk.TTkLabel(text="This is page 3"))
500
page3_layout.addWidget(ttk.TTkCheckbox(text="Option"))
501
page3.setLayout(page3_layout)
502
503
# Add tabs
504
tabs.addTab(page1, "General")
505
tabs.addTab(page2, "Settings")
506
tabs.addTab(page3, "Options")
507
508
# Handle tab changes
509
@ttk.pyTTkSlot(int)
510
def tab_changed(index):
511
print(f"Switched to tab: {tabs.tabText(index)}")
512
513
tabs.currentChanged.connect(tab_changed)
514
515
root.mainloop()
516
```
517
518
### Application Template Example
519
520
```python
521
import TermTk as ttk
522
523
root = ttk.TTk()
524
525
# Create application template
526
app = ttk.TTkAppTemplate(parent=root, title="My Application")
527
528
# Set up menu bar
529
menu_bar = app.menuBar()
530
file_menu = menu_bar.addMenu("File")
531
file_menu.addAction("New", lambda: print("New"))
532
file_menu.addAction("Open", lambda: print("Open"))
533
file_menu.addSeparator()
534
file_menu.addAction("Exit", app.close)
535
536
edit_menu = menu_bar.addMenu("Edit")
537
edit_menu.addAction("Cut", lambda: print("Cut"))
538
edit_menu.addAction("Copy", lambda: print("Copy"))
539
edit_menu.addAction("Paste", lambda: print("Paste"))
540
541
# Set up central widget
542
central = ttk.TTkContainer()
543
layout = ttk.TTkVBoxLayout()
544
layout.addWidget(ttk.TTkLabel(text="Main Content Area"))
545
layout.addWidget(ttk.TTkTextEdit())
546
central.setLayout(layout)
547
app.setCentralWidget(central)
548
549
# Set up status bar
550
status_bar = app.statusBar()
551
status_bar.showMessage("Ready")
552
553
root.mainloop()
554
```