File: deferred.py

package info (click to toggle)
python-traitsui 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 13,292 kB
  • sloc: python: 39,867; makefile: 120; sh: 5
file content (65 lines) | stat: -rw-r--r-- 2,635 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#  Copyright (c) 2007, Enthought, Inc.
#  License: BSD Style.

#--(Deferred UI Notifications)--------------------------------------------------
"""
Deferred UI Notifications
=========================

In Traits 3.0, a change has been made to the way that events are handled in the
Traits UI that should improve the performance and responsive of Traits-based
user interfaces in certain important situations.

In particular, changes made to the underlying model being displayed by a Traits
UI are no longer reflected immediately in the user interface, but are instead
queued up to be processed by the UI thread at its first available opportunity.

More precisely, the first time that a user interface related model trait is
modified, an event requesting a user interface update is generated containing
the old and new values of the trait. Subsequent changes to the same model trait
result in no new events being generated, but instead simply update the original
event information with the latest value of the modified trait.

Eventually the UI thread will process the original event, at which point it will
update the user interface using the original old value of the trait along with
the latest new value.

Although this may sound like it should slow down user interface updates, in many
cases where a model is being rapidly updated by calculations running either on a
background or UI thread, it should actually appear to make the system more
responsive, and should in fact, help prevent or reduce situations where the user
interface would previously have appeared to be unresponsive due to an excessive
number of screen updates.
"""

#--<Imports>--------------------------------------------------------------------

from traits.api import *
from traitsui.api import *

#--[Count Class]----------------------------------------------------------------

class Count ( HasTraits ):

    count = Int
    go    = Button( 'Count' )

    view = View(
        Item( 'count', style = 'readonly' ),
        Item( 'go', show_label = False )
    )

    def _go_changed ( self ):
        # Even though the 'count' trait (which is visible in the UI) is being
        # rapidly updated here, the UI should show only a single update each
        # time the 'Count' button is clicked. In previous Traits versions, the
        # user would actually see the counter update sequentially through all
        # 10,000 values, during which time the user interface would be
        # unresponsive:
        for i in range( 10000 ):
            self.count += 1

#--<Example*>-------------------------------------------------------------------

demo = Count()