Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use SWT from Jython?

The SWT-Gui looks very nice. Is there an easy way to use it in Jython ?

like image 797
Natascha Avatar asked Feb 05 '09 16:02

Natascha


3 Answers

Given that you can use all Java classes from within Jython, it is also possible to use SWT.

For the example, adapted from an SWT snippet, make sure you have the SWT jar on your CLASSPATH:

import org.eclipse.swt as swt
import org.eclipse.swt.widgets as widgets
import org.eclipse.swt.layout as layout

result = None

display = widgets.Display()

shell = widgets.Shell(display)
shell.pack()
shell.open()

dialog = widgets.Shell(shell, swt.SWT.DIALOG_TRIM | swt.SWT.APPLICATION_MODAL)
dialog.setLayout(layout.RowLayout())

ok = widgets.Button(dialog, swt.SWT.PUSH)
ok.setText ("OK")
cancel = widgets.Button(dialog, swt.SWT.PUSH);
cancel.setText("Cancel");

class MyListener(widgets.Listener):
    def handleEvent(self, event):
        global result
        result = event.widget == ok
        dialog.close()

listener = MyListener()
ok.addListener(swt.SWT.Selection, listener)
cancel.addListener(swt.SWT.Selection, listener)

dialog.pack()
dialog.open()
while not dialog.isDisposed():
    if not display.readAndDispatch():
        display.sleep ()
print "Result:", result
display.dispose()
like image 81
Torsten Marek Avatar answered Oct 22 '22 14:10

Torsten Marek


Jython has a few other niceties that makes the code cleaner.

Jython automagically translates getters & setters into public properties so that

ok.setText ("OK")

becomes simply

ok.text = 'OK'

You can then supply them as named arguments to the constructor. Jython also handles creating listener objects for your event handlers:

def handleEvent(self, event):
    global result
    result = event.widget == ok
    dialog.close()

ok = widgets.Button(dialog, swt.SWT.PUSH
        text='OK',
        widgetSelected=handleEvent)
cancel = widgets.Button(dialog, swt.SWT.PUSH
        text='Cancel',
        widgetSelected=handleEvent)
like image 29
Peter Gibson Avatar answered Oct 22 '22 15:10

Peter Gibson


Actually, there is no need for a special module. This talk by Sean McGrath contains a simple example of a Jython/SWT GUI.

Slide 11 of the talk begins with:

"""
Simple SWT Example
Sean McGrath
"""
from org.eclipse.swt.events import *
from org.eclipse.swt.graphics import *
from org.eclipse.swt.layout import *
from org.eclipse.swt.widgets import *
from org.eclipse.swt.layout.GridData import *
from org.eclipse.swt import *

It shows that SWT is directly usable from Jython. The full example is right there at Sean's site.

like image 5
gimel Avatar answered Oct 22 '22 13:10

gimel