File: 16_mousetrap.py

package info (click to toggle)
python-trame 3.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 101,620 kB
  • sloc: python: 13,515; sh: 183; javascript: 93; makefile: 7
file content (66 lines) | stat: -rw-r--r-- 1,534 bytes parent folder | download | duplicates (2)
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
66
from trame.app import get_server
from trame.ui.html import DivLayout
from trame.widgets import trame

# -----------------------------------------------------------------------------
# Trame app
# -----------------------------------------------------------------------------

server = get_server()
state = server.state

state.last_action = ""
state.action_count = 1


def save():
    print("Save")
    state.action_count += 1
    state.last_action = "Save"


def open():
    print("Open")
    state.action_count += 1
    state.last_action = "Open"


def edit():
    print("Edit")
    state.action_count += 1
    state.last_action = "Edit"


def esc():
    print("Escape")
    state.action_count += 1
    state.last_action = "Escape"


# -----------------------------------------------------------------------------
# UI setup
# -----------------------------------------------------------------------------
layout = DivLayout(server)

with layout:
    mt = trame.MouseTrap(
        Save=save,
        Open=open,
        Edit=edit,
        Escape=esc,
    )
    layout.root.add_child("{{ last_action }} #{{action_count}}")


# Do binding after
mt.bind(["ctrl+s", "mod+s"], "Save", stop_propagation=True)
mt.bind(["ctrl+o", "mod+o"], "Open", stop_propagation=True)
mt.bind("mod+e", "Edit")
mt.bind("esc", "Escape")

# -----------------------------------------------------------------------------
# start server
# -----------------------------------------------------------------------------

if __name__ == "__main__":
    server.start()