Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lemerchand opened this issue Mar 2, 2023 · 3 comments
Open

Comments

@lemerchand
Copy link

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
Copy link
Collaborator

jheinen commented Mar 3, 2023

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
Copy link
Author

lemerchand commented Mar 3, 2023

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
Copy link
Collaborator

jheinen commented Mar 6, 2023

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants