Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine JavaFX with Python

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.

like image 555
ldg Avatar asked Mar 25 '17 16:03

ldg


1 Answers

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

like image 66
davidrmcharles Avatar answered Sep 22 '22 20:09

davidrmcharles