Combine JavaFX with Python

时光毁灭记忆、已成空白 提交于 2019-12-06 04:39:13

问题


I was wondering if it is possible to design a GUI using JavaFX and afterwards combining with some Python code (for example make a button using JavaFX and then write handler code in Python to give some functionality).

JavaFX is great to design a really good GUI and I need Python to control a robot (the libraries are only available in Python).

I had a look over the web and I found Jython, but I could not understand if it will allow me to use these third parties Python libraries.

Does anyone have a good suggestion or any sources where I can look at? Any information would be appreciated.

Thank you in advance.


回答1:


Yes, you can write your JavaFX UI in Python (Jython):

#!/usr/bin/env jython
'''
"Hello, World!" in Jython and JavaFX

Roughly based on this: http://docs.oracle.com/javafx/2/get_started/hello_world.htm
'''

import sys

from javafx.application import Application

class HelloWorld(Application):

    @classmethod
    def main(cls, args):
        HelloWorld.launch(cls, args)

    def start(self, primaryStage):
        primaryStage.setTitle('Hello World!')

        from javafx.scene import Scene
        from javafx.scene.layout import StackPane
        primaryStage.setScene(Scene(StackPane(), 320, 240))
        primaryStage.show()

if __name__ == '__main__':
    HelloWorld.main(sys.argv)

That is quite easy. I am doing it.

You can also write your JavaFX UI in Java, and use something like the object factories to dispatch control to your Python (Jython) code. More on that here: http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html#using-jython-within-java-applications



来源:https://stackoverflow.com/questions/43018661/combine-javafx-with-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!