| 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 |
Displaying a statusbar in a Traits UI |
| 13 |
|
| 14 |
A statusbar may contain one or more fields, of fixed or variable size. |
| 15 |
|
| 16 |
Fixed width fields are specified in pixels, while variable width fields are |
| 17 |
specified as fractional values relative to other variable width fields. |
| 18 |
|
| 19 |
The content of a statusbar field is specified via the extended trait name of |
| 20 |
the object attribute that will contain the statusbar information. |
| 21 |
|
| 22 |
In this example, there are two statusbar fields: |
| 23 |
|
| 24 |
- The current length of the text input data (variable width) |
| 25 |
- The current time (fixed width, updated once per second). |
| 26 |
|
| 27 |
Note the use of a timer thread to update the status bar once per second. |
| 28 |
|
| 29 |
""" |
| 30 |
|
| 31 |
from time import sleep, strftime |
| 32 |
from threading import Thread |
| 33 |
from traits.api import HasPrivateTraits, Str, Property |
| 34 |
from traitsui.api import View, Item, StatusItem, Label |
| 35 |
|
| 36 |
# -- The demo class ------------------------------------------------------- |
| 37 |
|
| 38 |
|
| 39 |
class TextEditor(HasPrivateTraits): |
| 40 |
|
| 41 |
# The text being edited: |
| 42 |
text = Str() |
| 43 |
|
| 44 |
# The current length of the text being edited: |
| 45 |
length = Property(observe='text') |
| 46 |
|
| 47 |
# The current time: |
| 48 |
time = Str() |
| 49 |
|
| 50 |
# The view definition: |
| 51 |
view = View( |
| 52 |
Label('Type into the text editor box:'), |
| 53 |
Item('text', style='custom', show_label=False), |
| 54 |
title='Text Editor', |
| 55 |
id='traitsui.demo.advanced.statusbar_demo', |
| 56 |
width=0.4, |
| 57 |
height=0.4, |
| 58 |
resizable=True, |
| 59 |
statusbar=[ |
| 60 |
StatusItem(name='length', width=0.5), |
| 61 |
StatusItem(name='time', width=85), |
| 62 |
], |
| 63 |
) |
| 64 |
|
| 65 |
# -- Property Implementations --------------------------------------------- |
| 66 |
|
| 67 |
def _get_length(self): |
| 68 |
return 'Length: %d characters' % len(self.text) |
| 69 |
|
| 70 |
# -- Default Trait Values ------------------------------------------------- |
| 71 |
|
| 72 |
def _time_default(self): |
| 73 |
thread = Thread(target=self._clock) |
| 74 |
thread.daemon = True |
| 75 |
thread.start() |
| 76 |
|
| 77 |
return '' |
| 78 |
|
| 79 |
# -- Private Methods ------------------------------------------------------ |
| 80 |
|
| 81 |
def _clock(self): |
| 82 |
"""Update the statusbar time once every second.""" |
| 83 |
while True: |
| 84 |
self.time = strftime('%I:%M:%S %p') |
| 85 |
sleep(1.0) |
| 86 |
|
| 87 |
|
| 88 |
# Create the demo object: |
| 89 |
popup = TextEditor() |
| 90 |
|
| 91 |
# Run the demo (if invoked from the command line): |
| 92 |
if __name__ == '__main__': |
| 93 |
popup.configure_traits() |