EzDevInfo.com

enaml

Declarative User Interfaces for Python

Enaml: allow Window to be resizeable

If i use the Window widget, it is not resizable and fixed to its container size. How can i set the Window to be resizable? Following is not resizable:

enamldef MyWindow(Window)
    VGroup: 
        MPLCanvas:
            figure = Figure()
        CheckBox:
            text = "Show current"
        CheckBox:
            text = "Show mean"
        CheckBox:
            text = "Show first detector"

Source: (StackOverflow)

Create OK/Cancel dialog

It should be so simple, but I have not been able to figure out how to do a simple OK/cancel confirmation dialog in enaml. Could someone please enlighten me? I am using an ETS toolkit of Qt4 with pyside api, python 2.7, and enaml 0.6.8.

My application consists of a MainWindow and is launched like the following:


from enaml.stdlib.sessions import simple_session
from enaml.qt.qt_application import QtApplication

...

session = simple_session('myApp',...)

app = QtApplication([session])
app.start_session('myApp')
app.start()

Thanks in advance


Source: (StackOverflow)

Advertisements

How to get key events when using enaml?

I've been using enaml (0.6.8 which is what is available with Canopy for now) and have successfully created some very useful utility applications. I would like, however, to intercept keyboard events to enable some quick keyboard shortcuts rather than repetitive button-clicking in the UI.

How do I approach this? I understand that this will be toolkit (qt4) specific, but cannot really figure out where to begin. I've read some on event filters in qt, which seem like what I might want, but I do not understand the mechanisms for relating QApplication, etc. to enaml


Source: (StackOverflow)

Python Enaml "id: invalid syntax"

I am trying to use id's in an enaml file and when I run the program with

enaml-run hello_world.enaml 

I get this output

File "hello_world.enaml", line 10
    id: pb1
SyntaxError: invalid syntax

The output always produces an invalid syntax error on the id. How do I properly identity id's?

Source:

from enaml.layout.api import vbox, hbox, spacer, align
from enaml.widgets.api import (Window, Container, GroupBox, Form, PushButton, 
    CheckBox, RadioButton, Label, ScrollArea, ToolBar, Action, ActionGroup, 
    Splitter, Field)

enamldef Left(Container):
    GroupBox:
        title="Consoles"
        PushButton:
            id: pb1
            text="hello"

enamldef Right(Container):
    Label:
        text="Yo"

enamldef Bottom(ToolBar):
    PushButton:
        Field:
            text="Search..."

enamldef Main(Window):
    title="RetroArch"
    initial_size = (800,600)
    Container:
        Splitter:
            Left:lt:
                pass
            Right:rt:
                pass
        Bottom:
            pass

Source: (StackOverflow)

How to accomplish initial Splitter and SplitItem layout with enaml?

I'm using the enaml toolkit and would like to know how to initialize Splitter / SplitItem layouts.

Below is some very simple sample code. I'd like the window to start with the left SplitItem taking about 2/3 of the window width with the right SplitItem getting the other third. I've tried a variety of constraints in a variety of places but can't seem to hit on what I need to do.

Window starts like this: equal width split items

I want it to start like this: left split item wider

from enaml.widgets.api import (
    Window, Container, Splitter, SplitItem, Html
    )

enamldef Left(Container):
    Html:
        source = '<center><h1>Hello Left!</h1></center>'

enamldef Right(Container):
    Html:
        source = '<center><h1>Hello Right!</h1></center>'

enamldef Main(Window):
    initial_size = (800,400)
    Container:

        Splitter:

            SplitItem:
                Left:lt:
                    pass

            SplitItem:
                Right:rt:
                    pass

Source: (StackOverflow)

Using enaml to create UI binding to string in textfile

Let's say that I want to use a custom markup to generate a GUI in enaml. Is it possible to bind variables from the input form to a textfile?

Sample Input:
template.ml

!MENUBAR
 !ITEM TEXT="NEW FILE"  ACTION="emacs -nw {{textinput}}" FORM=(@textinput@|"new document.txt")!END
!END

new document.txt is supposed to be a default value.

Is it possible to use enaml like this?


Source: (StackOverflow)

How do I import show_simple_view from enaml.stdlib.sessions?

I'm trying to teach myself Enaml but none of the tutorial code works for me. http://docs.enthought.com/enaml/instructional/tut_hello_world.html

Whenever I execute the Python code, it stops at line 8:

from enaml.stdlib.sessions import show_simple_view

and gives the error:

ImportError: No module named sessions

I have Enaml and all of it's dependencies. What am I doing wrong?


Source: (StackOverflow)

How to get vertical slider in enaml?

Is it possible to do a vertical slider in enaml? I could not find any among the examples or documentation. The layout.api only seems to order different elements, not change the orientation of a given element.


Source: (StackOverflow)

Enaml slider arrow keys not working

I'm using enaml and trying to get a slider that can also be controlled with the arrow keys. I've used the slide example from the gallery (which doesn't work out-of-the-box, and gives a math domain error, but can be fixed by removing the log). According to the slider documentation there is a single_step property that:

defines the number of steps that the slider will move when the user presses the arrow keys

This seems to have a default of 1, but in my case nothing happens when I press the arrow keys. I wrote it explicitly under Slider: (and tried using larger values), but to no avail. Nothing seems to happen when I press the arrow keys.

I'm using enable 0.6.8 from Canopy in OS X, with the Qt backend. I wonder if this is a backend limitation? I had similar issues with sliders not being able to be controlled with the keyboard with TraitsUI as well.


Source: (StackOverflow)

Resizing MainWindow, HGroup doesn't expand verticallly

The following simple layout doesn't expand vertically after resizing, only vertically. I already played around with hug_width, hug_height and partners without success. I also tried using constraints with hbox

What i am missing?

from enaml.widgets.api import MPLCanvas, MainWindow, HGroup, VGroup, CheckBox
enamldef PumpProbeViewer(MainWindow):
    HGroup:
        align_widths = False
        MPLCanvas: plot_wid:
            figure = Figure()
        VGroup: control:
            CheckBox:
                text = "Show current"
            CheckBox:
                text = "Show mean"
            CheckBox:
                text = "Show first detector"

Source: (StackOverflow)

How to make a custom container?

I want to make reusable Container with a title in the like in the banner example. (So it is like a custom GroupBox). So every element i add to the class should be added to the classes subcontainer, not to itself. See the code below, i marked the position where i just want to write the subelements.

from enaml.widgets.api import *
from enaml.layout.api import *
from enaml.styling import *

enamldef BannerSheet(StyleSheet):
    Style:
        element = 'Label'
        style_class = 'banner'
        Setter:
            field = 'background'
            value = ('lineargradient(x1: 0, y1:0, x2:0, y2:1, '
                        'stop: 0 #222222, stop: 0.5 #1A1A1A, stop: 1 #111111)')
        Setter:
            field = 'color'
            value = '#FFFFEF'
        Setter:
            field = 'padding'
            value = '5px'
        Setter:
            field = 'font'
            value = '18pt Verdana'

enamldef Banner(Container):
    BannerSheet:
        pass
    padding = 0
    alias title : lbl.text
    constraints = [lbl.left == left, 
                   lbl.right == right, 
                   con.top == lbl.bottom,
                   con.bottom<=bottom]
    Label: lbl:
        name = 'abd'   
        style_class = 'banner'

    Container: con:
        pass


enamldef DemoContainer(Container):
    padding = 0
    Banner: b:
        title = 'Bar'
        #i want children here to be put into the banner.con container.
        #so e.g.
        Label:
             text = 'This should be below the title label"

Source: (StackOverflow)

How to get opencv image in an enaml space - Is it possible?

Is it possible to have enaml as target for OpenCV?

I'm thinking how to setup GUI and what to use. Nothing too complicated, I need to be able to set some bitmap background, draw rectangles and circles over it, but also have the possibility to select/move these graphics objects.

Also, I would like that I do not have to take care of all these elements when I stretch the window, etc. they should do this automatically since they would be defined in some "absolute" space. I think I could easily make it work for the bitmaps (even from memory), by overriding request_image in ImageProvider object (even though I see some strange cache happening in provider/enaml view).

Problem that I'm having now with OpenCV (OSX 64) is that even when I get resize to work with qt backend and CV_WINDOW_NORMAL, the content does not stretch. I like OpenCV, because easily I get basic UI functions.

On the other hand I started to like enaml so I'm thinking did anyone manage to get these to to work together. I'm thinking if link with MPL works, it's possible that coupling with OpenCV should be possible :)

Thanks!


Source: (StackOverflow)

How to use ScatterInspector and ScatterInspectorOverlay?

I would like to use the chaco tools ScatterInspector and/or ScatterInspectorOverlay with enaml. I've set up a very simple controller and view (source below) but cannot determine how to proceed. I have tried unsuccessfully to follow the minimal and old examples I've found.

If I uncomment the overlay part for ScatterInspectorOverlay, the code fails to run with

File ".../chaco/scatter_inspector_overlay.py", line 51, in overlay if not plot or not plot.index or not getattr(plot, "value", True):

If I comment out the overlay part, I of course don't get the overlay behavior I want and also, on moving the mouse, get

File ".../chaco/tools/scatter_inspector.py", line 48, in normal_mouse_move index = plot.map_index((event.x, event.y), threshold=self.threshold)

view.enaml source:

from enaml.widgets.api import (
        Window, Container, EnableCanvas,
    )

enamldef ScatterView(Window):
    attr controller
    title = "Scatter Inspector Test"
    initial_size = (640,480)

    Container:

        EnableCanvas:
            component = controller.scatter_plot

controller.py source:

import enaml
from enaml.stdlib.sessions import show_simple_view
from traits.api import HasTraits, Instance
from chaco.api import Plot, ArrayPlotData, ScatterInspectorOverlay
from chaco.tools.api import ScatterInspector
from numpy import linspace, sin

class ScatterController(HasTraits):
    scatter_plot = Instance(Plot)

    def _scatter_plot_default(self):
        # data
        x = linspace(-14, 14, 100)
        y = sin(x) * x**3
        plotdata = ArrayPlotData(x = x, y = y)

        # plot
        scatter_plot = Plot(plotdata)
        renderer = scatter_plot.plot(("x", "y"), type="scatter", color="red")

        # inspector
        scatter_plot.tools.append(ScatterInspector(scatter_plot))

        # overlay
        # scatter_plot.overlays.append( ScatterInspectorOverlay(
        #         scatter_plot,
        #         hover_color = 'red',
        #         hover_marker_size = 6,
        #         selection_marker_size = 6,
        #         selection_color = 'yellow',
        #         selection_outline_color='purple',
        #         selection_line_width = 3
        #     ))

        #return
        return scatter_plot

if __name__ == "__main__":
    with enaml.imports():
        from view import ScatterView

    main_controller = ScatterController()
    window = ScatterView(controller=ScatterController())
    show_simple_view(window)

Source: (StackOverflow)

Enaml 'hello world ' fails to run in Canopy editor

Trying to run of the shelf Hello World in Canopy editor - got the error bellow

In [1]: %run /home/smarkov/Enthought/Canopy_64bit/User/Examples/enaml-0.2.0/hello_world/hello_world.py
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/home/smarkov/Canopy/appdata/canopy-1.4.0.1938.rh5-x86_64/lib/python2.7/site-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
    202             else:
    203                 filename = fname
--> 204             __builtin__.execfile(filename, *where)

/home/smarkov/Enthought/Canopy_64bit/User/Examples/enaml-0.2.0/hello_world/hello_world.py in <module>()
      6 
      7 with enaml.imports():
----> 8     from hello_world_view import MyMessageToTheWorld
      9 
     10 view = MyMessageToTheWorld(message="Hello, world!")

/run/media/smarkov/Data/enaml-0.6.8/enaml/core/import_hooks.py in load_module(self, fullname)
    129         # module code of an Enaml file.
    130         with imports():
--> 131             exec code in mod.__dict__
    132         return mod
    133 

/home/smarkov/Enthought/Canopy_64bit/User/Examples/enaml-0.2.0/hello_world/hello_world_view.enaml in ()
      3 #  All rights reserved.
      4 #------------------------------------------------------------------------------
----> 5 enamldef MyMessageToTheWorld(MainWindow):
      6     attr message
      7     Container:

NameError: name 'MainWindow' is not defined 

Source: (StackOverflow)

Enthought enaml tableeditor

I have an object with a custom table editor attached to the view. This table editor can have some of its column deleted by user input. When it is the case I call an update function, this function is also called at the instantiation of the object.

class ModelList(HasTraits):

     models = List(Instance(Model))
     table_editor = TableEditor()
     view = View(Item(name='variables', show_label=False, editor=table_editor))

     def update(self) :
         columns = []
         for model in self.models :
             columns.append(ObjectColumn(..some stuff..))
         self.table_editor.columns = columns
         self.traits_modified = True

In the enaml layout this object is referred to like that:

enamldef Main(Window):
    attr model_list
    Container:
        TraitsItem: table:
            model := model_list

It works well at instantiation however when a column is deleted it stays in the view and the terminal is filled with AttributeErrors. Also in the view the column is filled with "Format!" key word. This is all logical since the view is querying an object that does not exist anymore.

So my question is, how can I make the view completely reloads itself following change of my ModelList object?

I have also tried to redefine edit_traits() since this is the method called by enaml while constructing the view, but the function is called only once at instantiation.

Thanks a lot by advance.


Source: (StackOverflow)