| 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 |
This demo illustrates use of the LEDEditor for displaying numeric values |
| 13 |
using a simulated LED display control. |
| 14 |
""" |
| 15 |
|
| 16 |
from threading import Thread |
| 17 |
|
| 18 |
from time import sleep |
| 19 |
|
| 20 |
from traits.api import HasTraits, Instance, Int, Bool, Float |
| 21 |
|
| 22 |
from traitsui.api import View, Item, HGroup, Handler, UIInfo, spring |
| 23 |
|
| 24 |
from traits.etsconfig.api import ETSConfig |
| 25 |
|
| 26 |
if ETSConfig.toolkit == 'wx': |
| 27 |
from traitsui.wx.extra.led_editor import LEDEditor |
| 28 |
else: |
| 29 |
from traitsui.qt.extra.led_editor import LEDEditor |
| 30 |
|
| 31 |
# Handler class for the LEDDemo class view: |
| 32 |
|
| 33 |
|
| 34 |
class LEDDemoHandler(Handler): |
| 35 |
|
| 36 |
# The UIInfo object associated with the UI: |
| 37 |
info = Instance(UIInfo) |
| 38 |
|
| 39 |
# Is the demo currently running: |
| 40 |
running = Bool(True) |
| 41 |
|
| 42 |
# Is the thread still alive? |
| 43 |
alive = Bool(True) |
| 44 |
|
| 45 |
def init(self, info): |
| 46 |
self.info = info |
| 47 |
Thread(target=self._update_counter).start() |
| 48 |
return True |
| 49 |
|
| 50 |
def closed(self, info, is_ok): |
| 51 |
self.running = False |
| 52 |
while self.alive: |
| 53 |
sleep(0.05) |
| 54 |
|
| 55 |
def _update_counter(self): |
| 56 |
while self.running: |
| 57 |
self.info.object.counter1 += 1 |
| 58 |
self.info.object.counter2 += 0.001 |
| 59 |
sleep(0.01) |
| 60 |
self.alive = False |
| 61 |
|
| 62 |
|
| 63 |
# The main demo class: |
| 64 |
|
| 65 |
|
| 66 |
class LEDDemo(HasTraits): |
| 67 |
|
| 68 |
# A counter to display: |
| 69 |
counter1 = Int() |
| 70 |
|
| 71 |
# A floating point value to display: |
| 72 |
counter2 = Float() |
| 73 |
|
| 74 |
# The traits view: |
| 75 |
view = View( |
| 76 |
Item( |
| 77 |
'counter1', |
| 78 |
label='Left aligned', |
| 79 |
editor=LEDEditor(alignment='left'), |
| 80 |
), |
| 81 |
Item( |
| 82 |
'counter1', |
| 83 |
label='Center aligned', |
| 84 |
editor=LEDEditor(alignment='center'), |
| 85 |
), |
| 86 |
Item( |
| 87 |
'counter1', |
| 88 |
label='Right aligned', |
| 89 |
editor=LEDEditor(), # default = 'right' aligned |
| 90 |
), |
| 91 |
Item( |
| 92 |
'counter2', |
| 93 |
label='Float value', |
| 94 |
editor=LEDEditor(format_str='%.3f'), |
| 95 |
), |
| 96 |
'_', |
| 97 |
HGroup( |
| 98 |
Item( |
| 99 |
'counter1', |
| 100 |
label='Left', |
| 101 |
height=-40, |
| 102 |
width=120, |
| 103 |
editor=LEDEditor(alignment='left'), |
| 104 |
), |
| 105 |
spring, |
| 106 |
Item( |
| 107 |
'counter1', |
| 108 |
label='Center', |
| 109 |
height=-40, |
| 110 |
width=120, |
| 111 |
editor=LEDEditor(alignment='center'), |
| 112 |
), |
| 113 |
spring, |
| 114 |
Item( |
| 115 |
'counter1', |
| 116 |
label='Right', |
| 117 |
height=-40, |
| 118 |
width=120, |
| 119 |
editor=LEDEditor(), # default = 'right' aligned |
| 120 |
), |
| 121 |
spring, |
| 122 |
Item( |
| 123 |
'counter2', |
| 124 |
label='Float', |
| 125 |
height=-40, |
| 126 |
width=120, |
| 127 |
editor=LEDEditor(format_str='%.3f'), |
| 128 |
), |
| 129 |
), |
| 130 |
title='LED Editor Demo', |
| 131 |
buttons=['OK'], |
| 132 |
handler=LEDDemoHandler, |
| 133 |
) |
| 134 |
|
| 135 |
|
| 136 |
# Create the demo: |
| 137 |
demo = LEDDemo() |
| 138 |
|
| 139 |
# Run the demo (if invoked from the command line): |
| 140 |
if __name__ == '__main__': |
| 141 |
demo.configure_traits() |