sciapp / gr

GR framework: a graphics library for visualisation applications
Other
329 stars 54 forks source link

Python: Is there a way to detect if a plot window has been closed? #173

Open lemerchand opened 1 year ago

lemerchand commented 1 year ago

Hi, I've scoured the docs and and github issues but haven't been able to solve my issue. TL;DR I'd like to know how I can tell if a GR window is open or closed. If you want some more details I've provided them below.

I'm working on an oscilloscope program that can look at units such as db/rms/volts/amplitude of a microphone that can be toggled by a button. As of right now the process looks like this:

  1. Run program
  2. Enable a microphone input
  3. Press 'Plot oscilloscope'
  4. Read measurements, and possibly: i. stop and save the audio ii. change the unit of measurement

As of right now, if I want to (b) change the unit of measurement I either have to (A):

  1. Disable the mic to stop input to the GR view port
  2. Close the GR window
  3. Modify the settings for the axis
  4. Repeat steps (3-4)

Or (B), set the program up to just always run the GR code when the mic enabled button is on. This works well as it allows me to change the units in realtime, or even leave the oscilloscope window up even when the mic is toggled off. However....

The issue with (B) is that I would like to have the option to enable the microphone without opening the oscilloscope window. If I could query the visibility of the plot window things would be simple as pie.

Does anyone have any suggestions?

jheinen commented 1 year ago

I think what you have in mind is best realized with a GUI application (e.g. in Qt5 or Qt6) with an embedded GR widget. This would give you complete control over the window management. You could capture the audio data asynchronously in a thread. The advantage of GR is that you can use it in different environments and languages.

lemerchand commented 1 year ago

Thanks for the reply, @jheinen. I should have also mentioned that this is a GUI application using TKinter. I can see how I have control over window management, just not specifically detecting if the user closes the GR window (via the 'X' icon, for example). Can you point me in the right direction there?

jheinen commented 1 year ago

Well, you could plot into an image in memory and display it in a Tk canvas.

import numpy as np
width, height = 800, 600
image = np.zeros((height, width, 4), np.uint8)
pointer = image.ctypes.data

# Draw something into the memory using gr
import gr
gr.beginprint('!{}x{}@{:x}.mem'.format(width, height, pointer))
gr.polyline([0, 1], [0, 1])
gr.endprint()

# Use the image memory, e.g. using PIL/Pillow
from PIL import Image, ImageTk
img = Image.fromarray(image, 'RGBA')

img.show()

# Alternatively, you could display the image in a Tk canvas

import tkinter
from tkinter import Tk

win = Tk()
win.geometry("800x600")

test = ImageTk.PhotoImage(img)

label = tkinter.Label(image=test)
label.image = test

# Position image
label.place(x=0, y=0)

win.mainloop()