| 1 |
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
|
| 11 |
""" |
| 12 |
Control the height and width of a Group |
| 13 |
|
| 14 |
TraitsUI does not permit explicit specification of the height or width of a |
| 15 |
Group (or its descendants). The workaround is to put each Group whose size you |
| 16 |
wish to control into its own View class, which can be an Item (hence can be |
| 17 |
size-controlled) in the larger View. Sometimes it is necessary to repeat such |
| 18 |
surgery at several levels to get the desired layout. |
| 19 |
|
| 20 |
We separate the left and right groups by a splitter (HSplit), and also |
| 21 |
make the window itself resizable. |
| 22 |
|
| 23 |
""" |
| 24 |
from numpy import linspace, pi, sin |
| 25 |
from traits.api import Code, HasTraits, Instance, Str, Int |
| 26 |
|
| 27 |
# UItem is Unlabeled Item |
| 28 |
from traitsui.api import ( |
| 29 |
View, |
| 30 |
Item, |
| 31 |
UItem, |
| 32 |
HSplit, |
| 33 |
InstanceEditor, |
| 34 |
VGroup, |
| 35 |
HGroup, |
| 36 |
) |
| 37 |
|
| 38 |
|
| 39 |
class InstanceUItem(UItem): |
| 40 |
"""Convenience class for including an Instance in a View""" |
| 41 |
|
| 42 |
style = Str('custom') |
| 43 |
editor = Instance(InstanceEditor, ()) |
| 44 |
|
| 45 |
|
| 46 |
class CodeView(HasTraits): |
| 47 |
"""Defines a sub-view whose size we wish to explicitly control.""" |
| 48 |
|
| 49 |
n = Int(123) |
| 50 |
code = Code() |
| 51 |
view = View( |
| 52 |
# This HGroup keeps 'n' from over-widening, by implicitly placing |
| 53 |
# a spring to the right of the item. |
| 54 |
HGroup(Item('n')), |
| 55 |
UItem('code'), |
| 56 |
resizable=True, |
| 57 |
) |
| 58 |
|
| 59 |
|
| 60 |
class VerticalBar(HasTraits): |
| 61 |
"""Defines a sub-view whose size we wish to explicitly control.""" |
| 62 |
|
| 63 |
a = Str('abcdefg') |
| 64 |
b = Int(123) |
| 65 |
view = View( |
| 66 |
VGroup( |
| 67 |
Item('a'), |
| 68 |
Item('b'), |
| 69 |
show_border=True, |
| 70 |
), |
| 71 |
) |
| 72 |
|
| 73 |
|
| 74 |
class BigView(HasTraits): |
| 75 |
"""Defines the top-level view. It contains two side-by-side panels (a |
| 76 |
vertical bar and a code editor under an integer) whose relative sizes we |
| 77 |
wish to control explicitly. If these were simply defined as Groups, we |
| 78 |
could not control their sizes. But by extracting them as separate classes |
| 79 |
with their own views, the resizing becomes possible, because they are |
| 80 |
loaded as Items now. |
| 81 |
""" |
| 82 |
|
| 83 |
bar = Instance(VerticalBar, ()) |
| 84 |
code = Instance(CodeView) |
| 85 |
view = View( |
| 86 |
HSplit( |
| 87 |
# Specify pixel width of each sub-view. (Or, to specify |
| 88 |
# proportionate width, use a width < 1.0) |
| 89 |
# We specify the height here for sake of demonstration; |
| 90 |
# it could also be specified for the top-level view. |
| 91 |
InstanceUItem('bar', width=150), |
| 92 |
InstanceUItem('code', width=500, height=500), |
| 93 |
show_border=True, |
| 94 |
), |
| 95 |
resizable=True, |
| 96 |
) |
| 97 |
|
| 98 |
|
| 99 |
x = linspace(-2 * pi, 2 * pi, 100) |
| 100 |
pv = CodeView(code=open(__file__).read()) |
| 101 |
bv = BigView(code=pv) |
| 102 |
bv.configure_traits() |