File: scrollbar_demo.py

package info (click to toggle)
python-enable 4.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,280 kB
  • ctags: 13,899
  • sloc: cpp: 48,447; python: 28,502; ansic: 9,004; makefile: 315; sh: 44
file content (61 lines) | stat: -rw-r--r-- 2,368 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
from enable.api import Container, Label, NativeScrollBar, Window
from enable.example_support import DemoFrame, demo_main


class MyFrame(DemoFrame):

    def _create_window(self):

        label = Label(text="h:\nv:", font="modern 16",
                      position=[20, 50],
                      bounds=[100, 100],
                      bgcolor = "red",
                      color = "white",
                      hjustify = "center",
                      vjustify = "center")

        vscroll = NativeScrollBar(orientation = "vertical",
                                  bounds = [15, label.height],
                                  position = [label.x2, label.y],
                                  range = (0, 100.0, 10.0, 1.0),
                                  enabled = True)
        vscroll.on_trait_change(self._update_vscroll, "scroll_position")

        hscroll = NativeScrollBar(orientation = "horizontal",
                                  bounds = [label.width, 15],
                                  position = [label.x, label.y-15],
                                  range = (0, 100.0, 10.0, 1.0),
                                  enabled = True)
        hscroll.on_trait_change(self._update_hscroll, "scroll_position")

        container = Container(bounds=[200,200], border_visible=True,
                              padding=15)
        container.add(label, hscroll, vscroll)
        container.on_trait_change(self._update_layout, "bounds")
        container.on_trait_change(self._update_layout, "bounds_items")

        self.label = label
        self.hscroll = hscroll
        self.vscroll = vscroll

        return Window(self, -1, component=container)

    def _update_hscroll(self):
        text = self.label.text.split("\n")
        text[0] = "h: " + str(self.hscroll.scroll_position)
        self.label.text = "\n".join(text)

    def _update_vscroll(self):
        text = self.label.text.split("\n")
        text[1] = "v: " + str(self.vscroll.scroll_position)
        self.label.text = "\n".join(text)

    def _update_layout(self):
        self.vscroll._widget_moved = True
        self.hscroll._widget_moved = True


if __name__ == "__main__":
    # Save demo so that it doesn't get garbage collected when run within
    # existing event loop (i.e. from ipython).
    demo = demo_main(MyFrame, title="Scrollbar demo", size=(250,250))