我有这样的 JavaFX 示例 Jython 代码:
# hello.py
from javafx.application import Application
from javafx.scene import Scene
import javafx.scene.control
from javafx.scene.layout import AnchorPane
class Hello(Application):
def start(self, stage):
stage.setTitle("Hello, World!")
root = AnchorPane()
label = javafx.scene.control.Label("Hello, World!")
root.getChildren().add(label)
scene = Scene(root, 100, 40)
stage.setScene(scene)
stage.show()
if __name__ == '__main__':
Application.launch(Hello().class, [])
在这里,我可以执行 import javafx.scene.control 然后像 javafx.scene.control.Label("Hello, World!") 一样使用它,但是为什么我不能做 from javafx.scene.control import Label 吗?
这是 Jython 信息:
$ jython
Jython 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:48:36)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_25
Type "help", "copyright", "credits" or "license" for more information.
>>>
请您参考如下方法:
如果您将 from/import 语句移动到您的 start 方法中,就在您需要 Label 之前> 控制,我打赌你的代码会工作。
在 JavaFX8 上运行 Jython 时,在 JavaFX 运行时启动之前,您无法执行某些操作(例如导入控件)。 (JavaFX2 没有这么挑剔。)因此,启动 JavaFX 运行时是您的 Jython/JavaFX 程序应该做的第一件事。 (我有一个专门用于执行此操作的模块。)然后您可以导入其他所有内容。
顺便说一句,我不认为你想使用 from/import 方法,而是 import/作为方法。前者打败了缓存,而后者则没有。
