6

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.

ldg
  • 450
  • 3
  • 7
  • 27
  • 1
    Jython and Python are the same language, but when it comes to libraries you can use ... then thy are much different beasts. I very much doubt that those libraries are available for Jython, to be so they should use zero dll, many interesting python library are partly written in either c++ or fortran. In this case jython cannot load them. – minus Mar 25 '17 at 23:38

1 Answers1

7

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

davidrmcharles
  • 1,923
  • 2
  • 20
  • 33
  • how do I install the module `javafx` in my python? I try to import it but gives a traceback saying No module named 'javafx'. – Rahat Zaman Aug 24 '18 at 07:02
  • You can't install it into *python*, but it is implicitly available to *jython* in some versions of Java, and you can manually add `jfxrt.jar` to your module search path if you are using a version of Java where it isn't available. (And, if you need more than that, you should probably open a new question.) – davidrmcharles Aug 24 '18 at 15:36
  • 2
    Problem here is that although Jython is a brilliant idea it has a tiny userbase, there are no signs of a v3, and the people in charge of the project give the impression of being something of a clique: no forum even, only a mailing list! I've used it and it works... but unless a miracle happens you'll always be learning a niche skill. – mike rodent Oct 19 '19 at 10:04