简体   繁体   English

如何在 Java Button (Swing/GUI) 单击上运行 python 代码?

[英]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.我用 Python 创建了一个程序,它打开网络摄像头并实时识别在相机帧中找到的人脸。 I feel it looks unpolished to run the python code from my IDE.我觉得从我的 IDE 运行 python 代码看起来很粗糙。 I want to execute the python code when the user clicks a button in my Java GUI form.我想在用户单击我的 Java GUI 表单中的按钮时执行 python 代码。

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.一个肮脏的黑客方法是调用Runtime.exec("python command here")并将侦听器附加到由此创建的进程。 This article explains the methods associated with this technique: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html .本文解释了与此技术相关的方法: 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.为什么不在 Python 中创建用户界面。 The popular GTK GUI library has Python bindings (docs at https://python-gtk-3-tutorial.readthedocs.io/en/latest/ ).流行的 GTK GUI 库具有 Python 绑定(文档位于https://python-gtk-3-tutorial.readthedocs.io/en/latest/ )。

Or consider writing the face recognition component in Java.或者考虑用 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.如果您完全从头开始编写它,这可能会很困难,但如果使用像 OpenCV 这样的库,则可能有可用的 Java 绑定。

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参考: 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.并等待py完成输出JSON格式,最后使用你想做的java rading JSON数据处理。

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.只需在按钮事件中使用另一个线程,这样您的 Java 程序主线程就不必等到事情完成并可以防止 UI 冻结。

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.如果您想将一些变量值传递给“python 命令”,只需在创建MyRunnable 类时创建一个构造函数,并将其作为参数传递给 MyRunnable 类的构造函数。

Now this will run a new thread when you click the button.现在,当您单击按钮时,这将运行一个新线程。 That would not mess with your main UI thread.这不会弄乱您的主 UI 线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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