| 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 |
A demo to demonstrate the progress column in a TableEditor. The demo only works |
| 13 |
with qt backend. |
| 14 |
""" |
| 15 |
|
| 16 |
import random |
| 17 |
|
| 18 |
from pyface.api import GUI |
| 19 |
from traits.api import Button, HasTraits, Instance, Int, List, observe, Str |
| 20 |
from traitsui.api import ObjectColumn, TableEditor, UItem, View |
| 21 |
from traitsui.extras.progress_column import ProgressColumn |
| 22 |
|
| 23 |
|
| 24 |
class Job(HasTraits): |
| 25 |
|
| 26 |
name = Str() |
| 27 |
|
| 28 |
percent_complete = Int() |
| 29 |
|
| 30 |
|
| 31 |
class JobManager(HasTraits): |
| 32 |
|
| 33 |
jobs = List(Instance(Job)) |
| 34 |
|
| 35 |
start = Button() |
| 36 |
|
| 37 |
def populate(self): |
| 38 |
self.jobs = [ |
| 39 |
Job(name='job %02d' % i, percent_complete=0) for i in range(1, 25) |
| 40 |
] |
| 41 |
|
| 42 |
def process(self): |
| 43 |
for job in self.jobs: |
| 44 |
job.percent_complete = min( |
| 45 |
job.percent_complete + random.randint(0, 3), 100 |
| 46 |
) |
| 47 |
|
| 48 |
if any(job.percent_complete < 100 for job in self.jobs): |
| 49 |
GUI.invoke_after(100, self.process) |
| 50 |
|
| 51 |
@observe('start') |
| 52 |
def _populate_and_process(self, event): |
| 53 |
self.populate() |
| 54 |
GUI.invoke_after(1000, self.process) |
| 55 |
|
| 56 |
traits_view = View( |
| 57 |
UItem( |
| 58 |
'jobs', |
| 59 |
editor=TableEditor( |
| 60 |
columns=[ |
| 61 |
ObjectColumn(name='name'), |
| 62 |
ProgressColumn(name='percent_complete'), |
| 63 |
] |
| 64 |
), |
| 65 |
), |
| 66 |
UItem('start'), |
| 67 |
resizable=True, |
| 68 |
) |
| 69 |
|
| 70 |
|
| 71 |
demo = JobManager() |
| 72 |
|
| 73 |
# Run the demo (if invoked from the command line): |
| 74 |
if __name__ == '__main__': |
| 75 |
demo.configure_traits() |