SWT-Gui看起来非常好看.有没有一种简单的方法在Jython中使用它?
鉴于您可以使用Jython中的所有Java类,也可以使用SWT.
对于从SWT片段改编的示例,请确保您的CLASSPATH上有SWT jar:
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()
Jython还有其他一些让代码更清晰的细节.
Jython自动将getter和setter转换为公共属性
ok.setText ("OK")
变得简单
ok.text = 'OK'
然后,您可以将它们作为命名参数提供给构造函数.Jython还处理为事件处理程序创建侦听器对象:
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)
实际上,不需要特殊模块.Sean McGrath的演讲包含了一个简单的Jython/SWT GUI示例.
演讲的幻灯片11开始于:
""" 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 *
它表明SWT可直接从Jython使用.在完整的例子就在那里,在肖恩的网站.