Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Currently I am executing the python code and sending command arrays with data using Runtime and exec() command calling the ".py" file. 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? Or on a python GUI which can then be embedded on a JPanel? 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. Or is using a Java based tool rather than basemap is more appropriate?(I haven't found any as good)

----Edit on 22nd May 2013------

Jython is not a solution as matplotlib is incompatible with it. Doing the whole thing in python I agree will be optimum but this is what I have to work with.

JACOB Jar: I wasn't able to find an example code showing how to embed a seperate application(basemap) on a JPanel or JFrame.

Currently I am planning on embedding the basemap into a wxpython GUI and then use sockets to communicate between the two languages.

TCP/IP Socket with Server Java and Client Python.

like image 378
javaEd Avatar asked Apr 22 '13 08:04

javaEd


People also ask

Can I use Java GUI with Python?

Using the Java Swing GUI package is the standard GUI toolkit for Java applications and is widely available on multiple platforms including Windows, Mac, and Linux. The event handlers in Java can then call existing Python applications to analyze the data.

Which is best GUI for Python or Java?

Python is an interpreted language and is all written in code (unlike Java's GUI or VB. net's GUI graphical construction), so making programs will be harder, but using a GUI library such as wxPython is somewhat daunting.

Can we create GUI in Python?

Creating a simple graphical user interface (GUI) that works across multiple platforms can be complicated. But it doesn't have to be that way. You can use Python and the PySimpleGUI package to create nice-looking user interfaces that you and your users will enjoy!


1 Answers

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.

#!/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:

  • Python2 (or 3)
  • Pyglet

Python mockup of running as a submodule to Javav

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

For instance.

like image 190
Torxed Avatar answered Oct 06 '22 05:10

Torxed