简体   繁体   中英

How do I run a python code on Java Button (Swing/GUI) click?

I have created a program in Python which opens the webcam and recognizes the faces found in the camera frame in real time. I feel it looks unpolished to run the python code from my IDE. I want to execute the python code when the user clicks a button in my Java GUI form.

Thanks in advance!, Ashwin

A dirty hacky way of doing this is to call Runtime.exec("python command here") and attach a listener to the process created by this. This article explains the methods associated with this technique: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html . A rough example would look like:

button.setOnAction(event -> {
    Runtime runtime = Runtime.getRuntime();
    Process process = runtime.exec("python command");
    process.getOutputStream()  // add handling code here
});

However, consider whether this is something you actually want to do. Why not create the user interface in Python. The popular GTK GUI library has Python bindings (docs at https://python-gtk-3-tutorial.readthedocs.io/en/latest/ ).

Or consider writing the face recognition component in Java. If you have written it purely from scratch this may be difficult, but if using a library like OpenCV, there are probably Java bindings available.

In general, without very special care, communicating cross-language is difficult and highly error-prone, so think very carefully about whether you need this exact setup.

I think you can using

    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec(path + "XXX.py");

ref: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html

and Waitting for py to finish output JSON format, last using java rading JSON data process you want to do.

Honestly to I guess the Answer given above is correct. Just use another thread inside the button event so your Java programs main thread wont have to wait till the thing finishes and could prevent the UI from freezing.

Create a Thread

public class MyRunnable implements Runnable {

           private String commandParameters = "";

           // Just Creating a Constructor 
           public MyRunnable(String cmd)
           {
                this.commandParameters = cmd;
           }

           public void run()
           {
             try
             {
               Runtime runtime = Runtime.getRuntime();
               // Custom command parameters can be passed through the constructor.
               Process process = runtime.exec("python " + commandParameters);
               process.getOutputStream();
             }
             catch(Exception e)
             {
                    // Some exception to be caught..
             }
           }   
}

And in Your Button Event do this

yourBtn.setOnAction(event -> {

   try{
     Thread thread = new Thread(new MyRunnable("command parameter string"));
     thread.start();
   }
   catch(Exception e)
   {
           // Some Expection..
   }

});

Now your main thread you not freeze or wait for the command execution to complete. Hope this solves the issue. if you want to pass some variable values to the "python command" just make you of a constructor while creating MyRunnable Class and pass the it as parameters to the constructor of the MyRunnable Class.

Now this will run a new thread when you click the button. That would not mess with your main UI thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM