Skip to content

Instantly share code, notes, and snippets.

@jason-s
Created April 9, 2020 16:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jason-s/65a2aebd412e73a5be64fee780231373 to your computer and use it in GitHub Desktop.
Observing return events in Field objects in enaml
#------------------------------------------------------------------------------
# Copyright (c) 2013, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#------------------------------------------------------------------------------
#
# mods by Jason Sachs
#
from enaml.layout.api import hbox, vbox, align, spacer
from enaml.widgets.api import Window, Label, Field, Form, Container
from enaml.stdlib.fields import IntField
def field_signal_return_event(field, signal):
field.proxy.widget.returnPressed.connect(lambda: signal.emit())
enamldef PersonForm(Form):
attr person
attr key_manager
Label:
text = 'First Name'
Field: fname:
text := person.first_name
Label:
text = 'Last Name'
Field: lname:
text := person.last_name
Label:
text = 'Age'
IntField: age:
minimum = 0
value := person.age
constraints = [
field_signal_return_event(age, person.ageSignal),
field_signal_return_event(fname, person.nameSignal),
field_signal_return_event(lname, person.nameSignal)
]
enamldef PersonView(Window):
attr person
PersonForm: form1:
person := parent.person
key_manager = keys
#------------------------------------------------------------------------------
# Copyright (c) 2013, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#------------------------------------------------------------------------------
#
# mods by Jason Sachs
#
from __future__ import print_function
from atom.api import Atom, Str, Range, Int, Bool, Signal, observe
import enaml
from enaml.qt.qt_application import QtApplication
class Person(Atom):
""" A simple class representing a person object.
"""
last_name = Str()
first_name = Str()
age = Range(low=0)
nameSignal = Signal()
ageSignal = Signal()
debug = Bool(False)
@observe('age')
def debug_print(self, change):
""" Prints out a debug message whenever the person's age changes.
"""
if self.debug:
templ = "{first} {last} is {age} years old."
s = templ.format(
first=self.first_name, last=self.last_name, age=self.age,
)
print(s)
@observe('nameSignal')
def namesig(self):
print("NAME SIGNAL")
@observe('ageSignal')
def agesig(self):
print("AGE SIGNAL")
if __name__ == '__main__':
with enaml.imports():
from person_view import PersonView
john = Person(first_name='John', last_name='Doe', age=42)
john.debug = True
app = QtApplication()
view = PersonView(person=john)
view.show()
app.start()
@jason-s
Copy link
Author

jason-s commented Apr 9, 2020

This modifies the Person tutorial to show how intercepting Return events in fields is pretty easy for the Qt backend. The relevant aspects of it are

def field_signal_return_event(field, signal):
    field.proxy.widget.returnPressed.connect(lambda: signal.emit())

and adding non-constraint function calls in the constraints section:

    constraints = [
		field_signal_return_event(age, person.ageSignal),
        field_signal_return_event(fname, person.nameSignal),
        field_signal_return_event(lname, person.nameSignal)
	]

where person.ageSignal and person.nameSignal are Signal objects defined in the model.

Then you can observe the signals and do whatever you want:

    @observe('nameSignal')
    def namesig(self):
        print("NAME SIGNAL")

    @observe('ageSignal')
    def agesig(self):
        print("AGE SIGNAL")

When you run test1.py and you press return in any of the fields, it will either print NAME SIGNAL for the first/last names, or AGE SIGNAL in the age field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment