【问题标题】:Python GUI from Java来自 Java 的 Python GUI
【发布时间】:2013-04-22 08:05:16
【问题描述】:

我正在开发一个程序,该程序接受用户输入并生成输出作为地图投影图。 我发现的最简单的地图投影库是 matplotlib-basemap,它是用 python 编写的,这是一种我不太熟悉的语言(我在 Java 上工作)。我用 Java 编写了用户界面。目前,我正在执行 python 代码并使用调用“.py”文件的运行时和 exec() 命令发送带有数据的命令数组。 这将执行命令并将绘图显示为单独的窗口。

我的问题是: 是否可以在 Jpanel 上嵌入此底图(与缩放功能交互)?或者在可以嵌入 JPanel 的 python GUI 上? 我知道我可以将 matplotlib 生成的图像保存为可以固定在面板上的文件,但是它不会是交互式的,那么缩放功能将不可用。 还是使用基于 Java 的工具而不是底图更合适?(我还没有发现任何好的)

----2013 年 5 月 22 日编辑-----

Jython 不是解决方案,因为 matplotlib 与它不兼容。我同意在 python 中做所有事情是最佳的,但这是我必须使用的。

JACOB Jar:我找不到显示如何在 JPanel 或 JFrame 上嵌入单独应用程序(底图)的示例代码。

目前我正计划将底图嵌入到 wxpython GUI 中,然后使用套接字在两种语言之间进行通信。

带有服务器 Java 和客户端 Python 的 TCP/IP 套接字。

【问题讨论】:

  • 请提供一些代码进行实验。
  • 不是跨平台解决方案,但在 Windows 上这应该会有所帮助:SetParent function
  • 我不禁要问,在这里使用 Jython 可以帮助您吗?
  • 我对 Java 不太熟悉,但是否可以从 Python 脚本返回地图投影图,然后让您的 GUI 全部使用 Java?似乎您在将一个 GUI 嵌入另一个 GUI 时会遇到一些问题,但我可能是错的。还有 dc5553,我不相信这会有所帮助,您可以将 Jython 编译成 .jar,但这并没有多大帮助,而且我认为基本上破解 Java 解释器并从中提取内容会更加困难.

标签: java python runtime.exec matplotlib-basemap


【解决方案1】:

如果您愿意接受新想法和学习新事物。
您想加入两种语言并不是针对您的特定问题的解决方案,而是替代将所有内容合并到 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()

你只需要:


作为 Javav 的子模块运行的 Python 模型

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

例如。

【讨论】:

  • 谢谢@Torxed。虽然我同意这是一个解决方案(即丢弃整个 Java 组件),但我很想看到一个调用 Python 子进程并呈现 matplotlib 图形的 Java 实现(在本例中为可以与之交互的底图。
  • Yw,我添加了一个小模型,说明如何在 Java 下将 Python 脚本作为子进程运行并监听“实时”输入,从而为您提供交互和重新渲染位图/底图的选项。
  • @Torxed 是的,我知道在 Python 中做所有事情是最佳的,但是 Java-Python 接口是我必须使用的(工作/客户端相关的东西)。 Python 纯粹是因为 Matplotlib-basemap 的交互特性。我尝试在 Jpanel 上修复 png 图像,但它不是交互式的。谢谢。
【解决方案2】:

您当然可以将您的 GUI 嵌入到Jython,这是一个 Python 的 Java 实现。不幸的是,它不支持 matplotlib,因为它依赖于本机代码。您可以尝试使用 execnet 从 Jython 调用 Python。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-17
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多