简体   繁体   English

Java的Python GUI

[英]Python GUI from Java

I am working on a program which takes a user input and generates an output as a map projection plot. 我正在开发一个程序,该程序需要用户输入并生成输出作为地图投影图。 The easiest map projection library that I have found is matplotlib-basemap, written in python a language I am not much familier with (I work on Java ).I have written the user interface in Java. 我发现的最简单的地图投影库是matplotlib-basemap,它是用python编写的,我不太熟悉(我在Java上工作)这种语言。我已经用Java编写了用户界面。 Currently I am executing the python code and sending command arrays with data using Runtime and exec() command calling the ".py" file. 目前,我正在执行python代码,并使用运行时和exec()命令调用“ .py”文件来向命令数组发送数据。 This exectues the command and shows the plot as a separate window. 这将执行命令并将绘图显示为单独的窗口。

My Question is this: Is it possible to embed this basemap (Interactive with Zoom features) on a Jpanel? 我的问题是:是否可以在Jpanel上嵌入此底图(与Zoom功能交互)? Or on a python GUI which can then be embedded on a JPanel? 还是在可以嵌入到JPanel的python GUI上? I know I can save the image generated by matplotlib as a file which can be fixed on a panel, but then it won't be interactive, The Zoom features won't be available then. 我知道我可以将matplotlib生成的图像另存为可以固定在面板上的文件,但是这样就不会是交互式的,那么缩放功能将不可用。 Or is using a Java based tool rather than basemap is more appropriate?(I haven't found any as good) 还是使用基于Java的工具而不是使用底图更合适?(我还没有找到更好的工具)

----Edit on 22nd May 2013------ ---- 2013年5月22日编辑------

Jython is not a solution as matplotlib is incompatible with it. Jython不是解决方案,因为matplotlib与它不兼容。 Doing the whole thing in python I agree will be optimum but this is what I have to work with. 我同意在python中完成整个操作将是最佳选择,但这是我必须使用的方法。

JACOB Jar: I wasn't able to find an example code showing how to embed a seperate application(basemap) on a JPanel or JFrame. JACOB Jar:我无法找到示例代码来显示如何在JPanel或JFrame上嵌入单独的应用程序(底图)。

Currently I am planning on embedding the basemap into a wxpython GUI and then use sockets to communicate between the two languages. 目前,我正在计划将底图嵌入到wxpython GUI中,然后使用套接字在两种语言之间进行通信。

TCP/IP Socket with Server Java and Client Python. 带服务器Java和客户端Python的TCP / IP套接字。

This is if you're open to new ideas and learning new things. 这是如果您愿意接受新思想和学习新事物。
It's not a solution on your specific problem that you want to join two languages, this is a replacement for that idea to incorporate everything in Python. 您不想结合两种语言来解决您的特定问题,这是一种将所有内容都整合到Python中的想法的替代。

#!/usr/bin/python
import pyglet
from time import time, sleep

class Window(pyglet.window.Window):
    def __init__(self):
        super(Window, self).__init__(vsync = False)
        self.alive = 1

        self.click = None
        self.drag = False

        with open('map.png', 'rb') as fh:
            self.geodata_image = pyglet.image.load('map.png', file=fh)
            self.geo_info = self.geodata_image.width, self.geodata_image.height

    def on_draw(self):
        self.render()

    def on_mouse_press(self, x, y, button, modifiers):
        self.click = x,y

    def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
        if self.click:
            self.drag = True
            print 'Drag offset:',(dx,dy)

    def on_mouse_release(self, x, y, button, modifiers):
        if not self.drag and self.click:
            print 'You clicked here', self.click, 'Relese point:',(x,y)
            ## Do work on corindate
        else:
            print 'You draged from', self.click, 'to:',(x,y)
            ## Move or link two points, or w/e you want to do.
        self.click = None
        self.drag = False

    def render(self):
        self.clear()
        ## An alternative to only getting a region if you only want to show a specific part of
        ## the image:
        # subimage = self.geodata_image.get_region(0, 0, self.geo_info[0], self.geo_info[1])
        self.geodata_image.blit(0, 0, 0) # x, y, z from the bottom left corner
        self.flip()

    def on_close(self):
        self.alive = 0

    def run(self):
        while self.alive:
            self.render()

            ## self.dispatch_events() must be in the main loop
            ## or any loop that you want to "render" something
            ## Because it is what lets Pyglet continue with the next frame.
            event = self.dispatch_events()
            sleep(1.0/25) # 25FPS limit

win = Window()
win.run()

All you need is: 所有你需要的是:


Python mockup of running as a submodule to Javav 作为Javav的子模块运行的Python样机

import sys
for line in sys.stdin.readline():
    if line == 'plot':
        pass # create image here

For instance. 例如。

You can certainly embed your GUI in Jython , a Java implementation of Python. 您当然可以将GUI嵌入Jython (Python的Java实现)中。 Unfortunately it doesn't support matplotlib, as it relies on native code. 不幸的是,它不支持matplotlib,因为它依赖于本机代码。 You could try and use execnet to invoke Python from Jython. 您可以尝试使用execnet从Jython调用Python。

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

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