PyQt5: Looking for example of Embedding external app window in Application - python

I have a largeish application I'm developing in Python3 using PyQt5. I want to do something very much like what is being done here in PyQt4:
# -*- coding: utf-8 -*-
import atexit
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class XTerm(QX11EmbedContainer):
def __init__(self, parent, xterm_cmd="xterm"):
QX11EmbedContainer.__init__(self, parent)
self.xterm_cmd = xterm_cmd
self.process = QProcess(self)
self.connect(self.process,
SIGNAL("finished(int, QProcess::ExitStatus)"),
self.on_term_close)
atexit.register(self.kill)
def kill(self):
self.process.kill()
self.process.waitForFinished()
def sizeHint(self):
size = QSize(400, 300)
return size.expandedTo(QApplication.globalStrut())
def show_term(self):
args = [
"-into",
str(self.winId()),
"-bg",
"#000000", # self.palette().color(QPalette.Background).name(),
"-fg",
"#f0f0f0", # self.palette().color(QPalette.Foreground).name(),
# border
"-b", "0",
"-w", "0",
# blink cursor
"-bc",
]
self.process.start(self.xterm_cmd, args)
if self.process.error() == QProcess.FailedToStart:
print "xterm not installed"
def on_term_close(self, exit_code, exit_status):
print "close", exit_code, exit_status
self.close()
(from https://bitbucket.org/henning/pyqtwi...11/terminal.py)
which is: Embed an X11 shell terminal (xterm) into a tab of my application.
I understand that QX11EmbedContainer is not in PyQt5, and that some usage of createWindowContainer may be the new approach, but I cannot find any example of this in action to follow.
I would ideally like to see the above code ported to a fully functional example using PyQt5 conventions and methods, but any assistance in any form will be helpful.
I have looked at Embedding external program inside pyqt5, but there is only a win32 solution there, and I need to run on Linux and MacOS, with windows being an unnecessary nicety, if there was a generalized solution to fit all 3. It feels like this is close, but I cannot find the appropriate replacement for the win32gui module to use in a general implementation.
I have looked at example of embedding matplotlib in PyQt5 1, but that pertains to matplotlib in particular, and not the general case.
All pointers and help will be gratefully acknowledged.

Related

Identifying why a Python Qt program pauses when not visible on macOS

I have a Qt Python program that logs data over a serial port. I'd like this program to always log data while running even when the application is not visible. Currently, when the application is not visible, the logging will pause after about ~45 seconds. Once the application window becomes visible again, logging resumes. The logging portion of the code is in a second thread using QRunnable and QThreadPool.
I've tried searching for the cause (or solution), but have not had much luck. Part of my problem is that I'm not sure if this issue is related to the OS, IDE, language, etc.
High-level details:
OS: macOS 12.4
IDE: vscode
Language/frameworks: Python3 / Qt (pyside6)
Does anyone have any ideas on why this application/thread might be pausing? Is it possible to have the application to continue to log data even when it is not visible? My hope is that once I'm pointed in the right direction I'll be able to address the issue.
UPDATE
Example code
class LogSignals(QObject):
result = Signal(dict)
class LogWorker(QRunnable):
def __init__():
super().__init__()
self.signals = LogSignals()
def run(self):
try:
for i in range(N_LOG_SAMPLES):
result = self.getSerialData()
self.signals.result.emit(result)
except:
traceback.print_exc()
exctype, value = sys.exc_info()[:2]
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.threadpool = QThreadPool()
def startLog(self):
log_worker = LogWorker()
log_worker.signals.result.connect(self.updateLogData)
self.threadpool.start(log_worker)
#Slot()
def updateLogData(self, result: dict):
#save data
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
You would need to check if your application is active, and start or drop the logging accordingly. You can get this by using:
QWidget *QApplication::activeWindow()
This would return a nullptr if your application does not have an active window. So, you would write something like this:
if (application->activeWindow())
; // Start / Keep logging
else
; // Stop logging

How to detect when a foreign window embedded with QWidget.createWindowContainer closes itself?

I'm embedding another window into a Qt widget using PySide2.QtGui.QWindow.fromWinId(windowId). It works well, but it does not fire an event when the the original X11 window destroys it.
If I run the file below with mousepad & python3 embed.py and press Ctrl+Q, no event fires and I'm left with an empty widget.
How can I detect when the X11 window imported by QWindow.fromWinId is destroyed by its creator?
#!/usr/bin/env python
# sudo apt install python3-pip
# pip3 install PySide2
import sys, subprocess, PySide2
from PySide2 import QtGui, QtWidgets, QtCore
class MyApp(QtCore.QObject):
def __init__(self):
super(MyApp, self).__init__()
# Get some external window's windowID
print("Click on a window to embed it")
windowIdStr = subprocess.check_output(['sh', '-c', """xwininfo -int | sed -ne 's/^.*Window id: \\([0-9]\\+\\).*$/\\1/p'"""]).decode('utf-8')
windowId = int(windowIdStr)
print("Embedding window with windowId=" + repr(windowId))
# Create a simple window frame
self.app = QtWidgets.QApplication(sys.argv)
self.mainWindow = QtWidgets.QMainWindow()
self.mainWindow.show()
# Grab the external window and put it inside our window frame
self.externalWindow = QtGui.QWindow.fromWinId(windowId)
self.externalWindow.setFlags(QtGui.Qt.FramelessWindowHint)
self.container = QtWidgets.QWidget.createWindowContainer(self.externalWindow)
self.mainWindow.setCentralWidget(self.container)
# Install event filters on all Qt objects
self.externalWindow.installEventFilter(self)
self.container.installEventFilter(self)
self.mainWindow.installEventFilter(self)
self.app.installEventFilter(self)
self.app.exec_()
def eventFilter(self, obj, event):
# Lots of events fire, but no the Close one
print(str(event.type()))
if event.type() == QtCore.QEvent.Close:
mainWindow.close()
return False
prevent_garbage_collection = MyApp()
Below is a simple demo script that shows how to detect when an embedded external window closes. The script is only intended to work on Linux/X11. To run it, you must have wmctrl installed. The solution itself doesn't rely on wmctrl at all: it's merely used to get the window ID from the process ID; I only used it in my demo script because its output is very easy to parse.
The actual solution relies on QProcess. This is used to start the external program, and its finished signal then notifies the main window that the program has closed. The intention is that this mechanism should replace your current approach of using subprocess and polling. The main limitation of both these approaches is they will not work with programs that run themselves as background tasks. However, I tested my script with a number applications on my Arch Linux system - including Inkscape, GIMP, GPicView, SciTE, Konsole and SMPlayer - and they all behaved as expected (i.e. they closed the container window when exiting).
NB: for the demo script to work properly, it may be necessary to disable splash-screens and such like in some programs so they can embed themselves correctly. For example, GIMP must be run like this:
$ python demo_script.py gimp -s
If the script complains that it can't find the program ID, that probably means the program launched itself as a background task, so you will have to try to find some way to force it into the foreground.
Disclaimer: The above solution may work on other platforms, but I have not tested it there, and so cannot offer any guarantees. I also cannot guarantee that it will work with all programs on Linux/X11.
I should also point out that embedding external, third-party windows is not officially supported by Qt. The createWindowContainer function is only intended to work with Qt window IDs, so the behaviour with foreign window IDs is strictly undefined (see: QTBUG-44404). The various issues are documentented in this wiki article: Qt and foreign windows. In particular, it states:
A larger issue with our current APIs, that hasn't been discussed yet,
is the fact that QWindow::fromWinId() returns a QWindow pointer, which
from an API contract point of view should support any operation that
any other QWindow supports, including using setters to manipulate the
window, and connecting to signals to observe changes to the window.
This contract is not adhered to in practice by any of our platforms,
and the documentation for QWindow::fromWinId() doesn't mention
anything about the situation.
The reasons for this undefined/platform specific behaviour largely
boils down to our platforms relying on having full control of the
native window handle, and the native window handle often being a
subclass of the native window handle type, where we implement
callbacks and other logic. When replacing the native window handle
with an instance we don't control, and which doesn't implement our
callback logic, the behaviour becomes undefined and full of holes
compared to a regular QWindow.
So, please bear all that in mind when designing an application that relies on this functionality, and adjust your expectations accordingly...
The Demo script:
import sys, os, shutil
from PySide2.QtCore import (
Qt, QProcess, QTimer,
)
from PySide2.QtGui import (
QWindow,
)
from PySide2.QtWidgets import (
QApplication, QWidget, QVBoxLayout, QMessageBox,
)
class Window(QWidget):
def __init__(self, program, arguments):
super().__init__()
layout = QVBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
self.setLayout(layout)
self.external = QProcess(self)
self.external.start(program, arguments)
self.wmctrl = QProcess()
self.wmctrl.setProgram('wmctrl')
self.wmctrl.setArguments(['-lpx'])
self.wmctrl.readyReadStandardOutput.connect(self.handleReadStdOut)
self.timer = QTimer(self)
self.timer.setSingleShot(True)
self.timer.setInterval(25)
self.timer.timeout.connect(self.wmctrl.start)
self.timer.start()
self._tries = 0
def closeEvent(self, event):
for process in self.external, self.wmctrl:
process.terminate()
process.waitForFinished(1000)
def embedWindow(self, wid):
window = QWindow.fromWinId(wid)
widget = QWidget.createWindowContainer(
window, self, Qt.FramelessWindowHint)
self.layout().addWidget(widget)
def handleReadStdOut(self):
pid = self.external.processId()
if pid > 0:
windows = {}
for line in bytes(self.wmctrl.readAll()).decode().splitlines():
columns = line.split(maxsplit=5)
# print(columns)
# wid, desktop, pid, wmclass, client, title
windows[int(columns[2])] = int(columns[0], 16)
if pid in windows:
self.embedWindow(windows[pid])
# this is where the magic happens...
self.external.finished.connect(self.close)
elif self._tries < 100:
self._tries += 1
self.timer.start()
else:
QMessageBox.warning(self, 'Error',
'Could not find WID for PID: %s' % pid)
else:
QMessageBox.warning(self, 'Error',
'Could not find PID for: %r' % self.external.program())
if __name__ == '__main__':
if len(sys.argv) > 1:
if shutil.which(sys.argv[1]):
app = QApplication(sys.argv)
window = Window(sys.argv[1], sys.argv[2:])
window.setGeometry(100, 100, 800, 600)
window.show()
sys.exit(app.exec_())
else:
print('could not find program: %r' % sys.argv[1])
else:
print('usage: python %s <external-program-name> [args]' %
os.path.basename(__file__))

Embedding mayaVI within wxPython

UPDATE: I've placed print commands within the code to isolate the moment of error. The output for the below code is a b c d. I ended up switching to pyqt/pyside using the alternative code provided, but please let me know if I can help in any other way from my current setup in debugging this issue.
from numpy import ogrid, sin
from traits.api import HasTraits, Instance
from traitsui.api import View, Item
from mayavi.sources.api import ArraySource
from mayavi.modules.api import IsoSurface
from mayavi.core.ui.api import SceneEditor, MlabSceneModel
class MayaviView(HasTraits):
scene = Instance(MlabSceneModel, ())
print "a"
view = View(Item('scene', editor=SceneEditor(), resizable=True,
show_label=False),
resizable=True)
print "b"
def __init__(self):
print "z"
HasTraits.__init__(self)
x, y, z = ogrid[-10:10:100j, -10:10:100j, -10:10:100j]
scalars = sin(x*y*z)/(x*y*z)
src = ArraySource(scalar_data=scalars)
self.scene.engine.add_source(src)
src.add_module(IsoSurface())
#-----------------------------------------------------------------------------
# Wx Code
import wx
print "c"
class MainWindow(wx.Frame):
print "d"
def __init__(self, parent, id):
print "e"
wx.Frame.__init__(self, parent, id, 'Mayavi in Wx')
self.mayavi_view = MayaviView()
self.control = self.mayavi_view.edit_traits(
parent=self,
kind='subpanel').control
self.Show(True)
app = wx.PySimpleApp()
frame = MainWindow(None, wx.ID_ANY)
app.MainLoop()
Initial Post:
I'm trying to reproduce the official code on mayaVI's website for embedding in wxWidgets (Wx embedding example).
The first error occurs while using app = wx.PySimpleApp(), and I change it to app = wx.App(False) via an online suggestion.
After this step, the program runs without command line error, but hangs during the data visualization step (the python visualization window never opens up, but does appear as an icon).
To test that my modules were installed correctly, I used MayaVI's official Qt example (Qt embedding example) - and it worked perfectly.
Details: I'm running Pythonw v=2.7.14 within a conda environment, with wxPython v=4.0.1 (osx-cocoa phoenix) and mayaVI v=4.5.0, all via macOS High Sierra Version 10.13.3.
Any advice on this matter would be a huge help - let me know if I can answer anything myself.

How to grab frames from a frame grabber card?

I am trying to write a custom software (in python) to grab frames from a frame grabber card (Hauppauge WinTV-HVR-1900), and I cannot get it to work. The bundled WinTV software works perfectly, and I am able to grab frames from usb webcams, so I know the hardware works. I manage to capture something though, since my screen is completely black but changes to the right size and resolution, so my hunch is it is some sort of decoding problem.
Could anyone help me with an indication or provide a code example for frame extraction from such hardware, please? Should I write an entire custom driver? Or maybe use the VLC python bindings (since VLC does succeed in reading the video stream)?
EDIT:
Basically, my question comes down to this: How is my frame grabber different from a webcam, since the integrated hardware encoder yields an MPEG-2 stream? Shouldn't is behave just as my webcam?
The logic file of my attempt (QT framework using pyQt4 with python 2.7 on windows 7):
#-*- coding: utf-8 -*-
import sys
from VideoCapture import Device
#import Gui files and classes
import FloGui
import deviceDialog
# PyQT4 imports
from PyQt4 import QtGui, QtCore
class devicedialog(QtGui.QDialog, deviceDialog.Ui_streamDialog):
#the logic of the streaming device choice dialog
def __init__(self,parent=None):
super(devicedialog,self).__init__(parent)
self.setupUi(self)
self.retranslateUi(self)
self.index = None
i=0
self.camlist=list()
while True:
try:
cam = Device(i,0)
name = cam.getDisplayName()
dev = [name,i]
self.camlist.append(dev)
i+=1
del cam
except:
break
for j in xrange(len(self.camlist)):
item = QtGui.QListWidgetItem(self.camlist[j][0])
self.deviceListBox.addItem(item)
self.exec_()
def accept(self):
selected = self.deviceListBox.currentItem().text()
for k in xrange(len(self.camlist)):
if self.camlist[k][0]==selected:
self.index = k
QtGui.QDialog.accept(self)
class Flolog(QtGui.QMainWindow,FloGui.Ui_MainWindow):
"""
Flolog is inherited from both QtGui.QDialog and FloGui.Ui_MainWindow
"""
def __init__(self, parent=None):
"""
Initialization of the class. Call the __init__ for the super classes
"""
super(Flolog,self).__init__(parent)
self.setupUi(self)
self.retranslateUi(self)
self.connectActions()
def streamchoice(self):
streamdialog = devicedialog()
self.VideoWidget.stop()
self.VideoWidget.path = streamdialog.index
self.VideoWidget.load()
self.playButton.setEnabled(True)
def menuloadfile(self):
filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '.')
self.VideoWidget.stop()
self.VideoWidget.path = str(filename)
self.VideoWidget.load()
self.playButton.setEnabled(True)
def playbutton(self):
self.VideoWidget.play()
self.playButton.setEnabled(False)
def main(self):
self.show()
def quit_(self):
sys.exit(0)
def connectActions(self):
"""
Connect the user interface controls to the logic
"""
self.actionFile.triggered.connect(self.menuloadfile)
self.actionStream.triggered.connect(self.streamchoice)
self.actionQuit.triggered.connect(self.quit_)
self.playButton.clicked.connect(self.playbutton)
self.stopButton.clicked.connect(self.VideoWidget.stop)
if __name__=='__main__':
app = QtGui.QApplication(sys.argv)
Flologob = Flolog()
Flologob.main()
sys.exit(app.exec_())
I actually finally did solve this. The problem stemmed from the fact that one should first specify which video device should be used by the graphics driver, either manually or in the code. I solved this on Linux Ubuntu by switching devices via the v4l2 graphics driver. I know there is an equivalent maneuver to be performed on Windows.

Activating and Disabling button after process in python and pyGTK

Essentially, I am trying to make a button "active" first, run a process, and then after that process has finished running, disable the button again.
Using pyGTK and Python, the code in question looks like this...
self.MEDIA_PLAYER_STOP_BUTTON.set_sensitive(True) #Set button to be "active"
playProcess = Popen("aplay " + str(pathToWAV) + " >/dev/null 2>&1",shell=True) #Run Process
playProcess.wait() #Wait for process to complete
self.MEDIA_PLAYER_STOP_BUTTON.set_sensitive(False) #After process is complete, disable the button again
However, this does not work at all.
Any help would be greatly appreciated.
All is working normally (python 2.7.3). But if you call playProcess.wait() in gui thread - you freeze gui thread without redrawing (sorry, my english isn`t very well). And are you sure that you try to use subprocess.popen()? Maybe os.popen()?
My small test:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pygtk, gtk, gtk.glade
import subprocess
def aplay_func(btn):
btn.set_sensitive(True)
print "init"
playProcess = subprocess.Popen("aplay tara.wav>/dev/null 2>&1", shell=True)
print "aaa"
playProcess.wait()
print "bbb"
btn.set_sensitive(False)
wTree = gtk.glade.XML("localize.glade")
window = wTree.get_widget("window1")
btn1 = wTree.get_widget("button1")
window.connect("delete_event", lambda wid, we: gtk.main_quit())
btn1.connect("clicked", aplay_func)
window.show_all()
gtk.main()
Result:
init
aaa
bbb
And yes, button is working correctly. Sound too.

Categories