pyqt5 videowidget not showing in layout - pyqt5

I am writing a program with pyqt5 where pressing a button first cycles through some pictures then cycles through some videos.
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import *
import glob
import argparse
import sys
class MainWindow(QMainWindow):
def __init__(self,args):
super(MainWindow, self).__init__()
self.setWindowTitle('Navon test')
self.setWindowFlags(Qt.FramelessWindowHint)
# exit option for the menu bar File menu
self.exit = QAction('Exit', self)
self.exit.setShortcut('Ctrl+q')
# message for the status bar if mouse is over Exit
self.exit.setStatusTip('Exit program')
# newer connect style (PySide/PyQT 4.5 and higher)
self.exit.triggered.connect(app.quit)
self.setWindowIcon(QIcon('icon.ico'))
self.centralwidget = CentralWidget(args)
self.setCentralWidget(self.centralwidget)
def keyPressEvent(self, QKeyEvent):
if QKeyEvent.key() == Qt.Key_Escape:
QCoreApplication.instance().quit()
self.centralwidget.startvid()
class CentralWidget(QWidget):
def __init__(self,args):
super(CentralWidget, self).__init__()
self.layout = QVBoxLayout()
self.layout.setAlignment(Qt.AlignCenter)
self.setLayout(self.layout)
self.player = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.vw = QVideoWidget()
self.player.setVideoOutput(self.vw)
def startvid(self):
self.layout.addWidget(self.vw)
url= QUrl.fromLocalFile(glob.glob("videos/*")[0])
content= QMediaContent(url)
self.player.setMedia(content)
self.player.setVideoOutput(self.vw)
self.player.play()
if __name__== "__main__":
parser = argparse.ArgumentParser()
#~ parser.add_argument("-nb","--nobox",action="store_true", help="do not wait for the box connection")
args = parser.parse_args()
app = QApplication(sys.argv)
mainwindow = MainWindow(args)
#~ mainwindow.showFullScreen()
mainwindow.show()
sys.exit(app.exec_())
I tried to paste the minimal code. The thing is, I press the button nothing shows, although I used examples like this one PyQt5 - Can't play video using QVideoWidget to test if playing the video is ok, and these work. It's as if it is not adding the widget to the layout or something. Any idea what might be wrong?

I had to use QGraphicsView to achieve what I wanted, here is a fix:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import *
import glob
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle('Navon test')
self.setWindowFlags(Qt.FramelessWindowHint)
# exit option for the menu bar File menu
self.exit = QAction('Exit', self)
self.exit.setShortcut('Ctrl+q')
# message for the status bar if mouse is over Exit
self.exit.setStatusTip('Exit program')
# newer connect style (PySide/PyQT 4.5 and higher)
self.exit.triggered.connect(app.quit)
self.setWindowIcon(QIcon('icon.ico'))
self.centralwidget = VideoPlayer()
self.setCentralWidget(self.centralwidget)
def keyPressEvent(self, QKeyEvent):
if QKeyEvent.key() == Qt.Key_Escape:
self.centralwidget.phaseQuit(2)
self.centralwidget.play()
class VideoPlayer(QWidget):
def __init__(self, parent=None):
super(VideoPlayer, self).__init__(parent)
self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.videoItem = QGraphicsVideoItem()
self.videoItem.setSize(QSizeF(640, 480))
scene = QGraphicsScene(self)
graphicsView = QGraphicsView(scene)
scene.addItem(self.videoItem)
layout = QVBoxLayout()
layout.addWidget(graphicsView)
self.setLayout(layout)
self.mediaPlayer.setVideoOutput(self.videoItem)
self.counter = 0
def play(self):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
pass
else:
self.mediaPlayer.setMedia(QMediaContent(QUrl.fromLocalFile(glob.glob("videos/*")[self.counter])))
self.mediaPlayer.play()
self.counter += 1
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
player = MainWindow()
player.show()
sys.exit(app.exec_())

Related

How to get log and show it on GUI from multiprocessing work?

I try to get logs from multiprocessing work and show them on GUI.
Based on this document
gui.py:
from PyQt5 import QtCore, QtWidgets
import logging
from log_test import main
Signal = QtCore.pyqtSignal
Slot = QtCore.pyqtSlot
class Signaller(QtCore.QObject):
signal = Signal(str, logging.LogRecord)
class QtHandler(logging.Handler):
def __init__(self, slotfunc, *args, **kwargs):
super().__init__(*args, **kwargs)
self.signaller = Signaller()
self.signaller.signal.connect(slotfunc)
def emit(self, record):
s = self.format(record)
self.signaller.signal.emit(s, record)
class Worker(QtCore.QObject):
finished = Signal()
#Slot()
def start(self):
main()
self.finished.emit()
class Ui_Dialog(QtCore.QObject):
def __init__(self):
super().__init__()
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.setEnabled(True)
Dialog.resize(530, 440)
self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
self.verticalLayout.setObjectName("verticalLayout")
self.button = QtWidgets.QPushButton(Dialog)
self.button.setText("start working")
self.verticalLayout.addWidget(self.button)
self.logWidget = QtWidgets.QPlainTextEdit(Dialog)
self.logWidget.setReadOnly(True)
self.verticalLayout.addWidget(self.logWidget)
self.handler = QtHandler(self.update_log_gui)
logging.getLogger('log').addHandler(self.handler)
self.button.clicked.connect(self.start_work)
#Slot(str, logging.LogRecord)
def update_log_gui(self, status, record):
self.logWidget.appendPlainText(status)
def config_thread(self):
self.worker_thread = QtCore.QThread()
self.worker_thread.setObjectName('WorkerThread')
self.worker = Worker()
self.worker.moveToThread(self.worker_thread)
self.worker_thread.started.connect(self.worker.start)
self.worker.finished.connect(self.worker_thread.quit)
self.worker.finished.connect(self.worker.deleteLater)
self.worker_thread.finished.connect(self.worker_thread.deleteLater)
self.worker_thread.finished.connect(lambda: self.button.setEnabled(True))
pass
def start_work(self):
self.config_thread()
self.worker_thread.start()
self.button.setEnabled(False)
if __name__ == "__main__":
import sys
QtCore.QThread.currentThread().setObjectName('MainThread')
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
log_test.py (where multiprocessing work happens)
import logging
import time
from multiprocessing import Pool
def f(name):
logger = logging.getLogger('log.' + name)
logger.error('hello there 1')
time.sleep(0.5)
logger.error('hello there 2')
time.sleep(0.5)
logger.error('hello there 3')
time.sleep(0.5)
def main():
with Pool(5) as p:
p.map(f, ['aaa', 'bbb', 'ccc'])
At first time, I thought working in single thread causing the problem. So I added QThread to this.
Later I discovered in debug, it seems to QtHandler.emit() works fine at receiving log messages. But the connected slot function, update_log_gui() does not work somehow.
I solved it myself.
#Alexander was right. Indeed my QtHandler has a problem when multiprocessing but I don't know exactly why. Rather, you wanna implement QueueHandler. An example in this article (Written in Korean) helped me.
from PyQt5 import QtCore, QtWidgets
import logging
import multiprocessing
from log_test import main
Signal = QtCore.pyqtSignal
Slot = QtCore.pyqtSlot
QThread = QtCore.QThread
class Signaller(QtCore.QObject):
signal = Signal(logging.LogRecord)
class Worker(QtCore.QObject):
finished = Signal()
def __init__(self, q):
super().__init__()
self.q = q
#Slot()
def start(self):
main(self.q)
self.finished.emit()
class Consumer(QThread):
popped = Signaller()
def __init__(self, q):
super().__init__()
self.q = q
self.setObjectName('ConsumerThread')
def run(self):
while True:
if not self.q.empty():
record = self.q.get()
self.popped.signal.emit(record)
class Ui_Dialog(QtCore.QObject):
def __init__(self, app):
super().__init__()
self.app = app
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.setEnabled(True)
Dialog.resize(530, 440)
self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
self.verticalLayout.setObjectName("verticalLayout")
self.button = QtWidgets.QPushButton(Dialog)
self.button.setText("start working")
self.verticalLayout.addWidget(self.button)
self.logWidget = QtWidgets.QPlainTextEdit(Dialog)
self.logWidget.setReadOnly(True)
self.verticalLayout.addWidget(self.logWidget)
self.button.clicked.connect(self.start_work)
self.q = multiprocessing.Manager().Queue()
self.consumer = Consumer(self.q)
self.consumer.popped.signal.connect(self.update_log_gui)
self.consumer.start()
app.aboutToQuit.connect(self.shutdown_consumer)
#Slot(logging.LogRecord)
def update_log_gui(self, record):
self.logWidget.appendPlainText(str(record.msg))
def config_thread(self):
self.worker_thread = QtCore.QThread()
self.worker_thread.setObjectName('WorkerThread')
self.worker = Worker(self.q)
self.worker.moveToThread(self.worker_thread)
self.worker_thread.started.connect(self.worker.start)
self.worker.finished.connect(self.worker_thread.quit)
self.worker.finished.connect(self.worker.deleteLater)
self.worker_thread.finished.connect(self.worker_thread.deleteLater)
self.worker_thread.finished.connect(lambda: self.button.setEnabled(True))
def start_work(self):
self.config_thread()
self.worker_thread.start()
self.button.setEnabled(False)
def shutdown_consumer(self):
if self.consumer.isRunning():
self.consumer.requestInterruption()
self.consumer.quit()
self.consumer.wait()
if __name__ == "__main__":
import sys
QtCore.QThread.currentThread().setObjectName('MainThread')
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog(app)
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())

unable to update matplotlib figure in pyqt5

I am studying MDIwindow in pyqt5
I want to update the ForcemapWidget figure when I click on the HeatmapWidget figure. However, the diagram did not update.
It is confirmed that the canvas in the ForcemapWidget is updated by the print function. We know that the coordinates are also obtained. But for some reason, the chart doesn't update.
import numpy as np
import matplotlib.pyplot as plt
from PyQt5.QtWidgets import QVBoxLayout,QApplication,QWidget,QMainWindow,QMdiArea,QAction,QMdiSubWindow,QTextEdit, \
QComboBox,QLineEdit,QPushButton,QCheckBox,QFormLayout
import sys
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
class MDIWindow(QMainWindow):
count=0
def __init__(self):
super ().__init__()
self.mdi=QMdiArea()
self.setCentralWidget(self.mdi)
bar=self.menuBar()
file=bar.addMenu("File")
file.addAction("New")
file.addAction("Cascade")
file.addAction("Tiled")
file.triggered[QAction].connect(self.tritri)
self.setWindowTitle("MDI Application")
# 最初のサブウィンドウを作成
sub=QMdiSubWindow()
form_widget=FormWidget()
sub.setWidget(form_widget)
sub.setWindowTitle("Sub Window 1")
self.mdi.addSubWindow(sub)
sub.show()
sub2=QMdiSubWindow()
# ForcemapWidget.force_update()
form_widget=ForcemapWidget()
# form_widget.update()
# print(form_widget)
sub2.setWidget (form_widget)
sub2.setWindowTitle("Sub Window 2")
self.mdi.addSubWindow(sub2)
sub2.show()
def tritri(self,p):
if p.text()=="New":
MDIWindow.count=MDIWindow.count+1
sub=QMdiSubWindow()
heatmap_widget=HeatmapWidget()
sub.setWidget(heatmap_widget)
sub.setWindowTitle("Sub Window"+str(MDIWindow.count))
self.mdi.addSubWindow(sub)
sub.show()
if p.text()=="Cascade":
self.mdi.cascadeSubWindows()
if p.text()=="Tiled":
self.mdi.tileSubWindows ()
class HeatmapWidget(QWidget):
def __init__(self):
super().__init__()
self.forcemapWidget=ForcemapWidget
fig,ax=plt.subplots (figsize = (1,1))
heatmap=np.random.rand (256,256)
im=ax.imshow(heatmap,cmap = "hot")
fig.colorbar(im)
ax.set_title("Heatmap")
fig.canvas.mpl_connect("button_press_event",self.onclick)
canvas=FigureCanvas(fig)
layout=QVBoxLayout()
layout.addWidget(canvas)
self.setLayout (layout)
def onclick(self,event):
x,y=event.xdata,event.ydata
forcemap=ForcemapWidget(x)
class ForcemapWidget (QWidget):
def __init__(self,x=None):
super().__init__()
self.figure=plt.figure()
self.initUI(x)
def initUI(self,x):
self.figure.clear()
ax=self.figure.add_subplot(111)
if x is not None:
heatmap=np.random.rand(int (x),256)
else:
heatmap=np.random.rand(256,256)
im=ax.imshow(heatmap,cmap = "hot")
# self.figure.colorbar (im)
canvas=FigureCanvas(self.figure)
layout=QVBoxLayout()
layout.addWidget(canvas)
self.setLayout(layout)
self.update()
class FormWidget(QWidget):
def __init__(self):
super().__init__()
form_layout = QFormLayout()
combo = QComboBox()
combo.addItems(["Option 1", "Option 2", "Option 3"])
form_layout.addRow("Dropdown:", combo)
line_edit = QLineEdit()
form_layout.addRow("Input:", line_edit)
button = QPushButton("Submit")
form_layout.addRow(button)
check_box = QCheckBox()
form_layout.addRow("Checkbox:", check_box)
self.setLayout(form_layout)
app = QApplication(sys.argv)
mdi = MDIWindow()
mdi.show()
app.exec_()

PyQt5 window opens then closes in a second

from PyQt5.QtWidgets import QApplication, QPushButton, QMainWindow
from PyQt5.QtGui import QIcon
import sys
class Main(QMainWindow):
def __init__(self, parent=None) -> None:
super().__init__(parent=parent)
self.setWindowTitle("QPushButton")
left = 300
top = 200
width = 300
height = 250
self.setGeometry(left, top, width, height)
self.show()
def main():
App = QApplication(sys.argv)
Main()
sys.exit(App.exec())
if __name__=="__main__":
main()
I don't get it, it worked before now it closes within a second of opening. I'm thinking somehow my installation got corrupted. Do you have any ideas I tried
def main():
App = QApplication(sys.argv)
Main()
App.exec_()

PyQt5 Signal Between Classes Causing Lockup

I'm trying to send signals between classes, which works fine from the HMI class to the WorkerThread class, but causes a program lockup, or infinite loop, when WorkerThread tries to connect to a signal from the HMI class.
import sys
import time
import PyQt5
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import QThread
from PyQt5.QtWidgets import QWidget, QApplication
from Home import Ui_HomeWin # PyQt5 Designer window
class WorkerThread(QThread):
hmiHandlesThis = PyQt5.QtCore.pyqtSignal(str)
def __init__(self, parent=None):
super(WorkerThread, self).__init__(parent)
self.counter = 0
# this attempted connection causes a lockup
self.HMI_Thread = HMI() # appears to be a recursive loop
self.HMI_Thread.updateGlobals.connect(self.update_global_widgets)
def run(self):
while True:
time.sleep(0.5)
print('Doing a bunch of other stuff: {}'.format(self.counter))
self.counter += 1
def build_a_command(self):
print('building a command...')
name = 'pbAutoMode'
# example command
command = name + '.setStyleSheet("QPushButton{ background-color: rgb(0, 0, 255); }")'
self.hmiHandlesThis.emit(command)
def update_global_widgets(self):
print('update some global widgets')
class HMI(QWidget):
updateGlobals = PyQt5.QtCore.pyqtSignal(name='updateGlobals')
def __init__(self, parent=None):
super(HMI, self).__init__(parent)
self.HomeWin = QtWidgets.QDialog()
self.HomeWin.setWindowFlags(QtCore.Qt.FramelessWindowHint)
ui = Ui_HomeWin()
ui.setupUi(self.HomeWin)
self.HomeWin.show()
# this connection works
self.workerThread = WorkerThread()
self.workerThread.hmiHandlesThis.connect(self.on_new_command)
self.workerThread.start()
def on_new_command(self, command):
print('New command is: {}'.format(command))
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = HMI()
sys.exit(app.exec_())~~~
I've tried placing the signal definitions inside the init functions, with no difference.
With Dennis Jensen's help, i've been able to get this running properly. I'm posting the working snippet here as an example of 'proper' thread building and the passing of signals.
import sys
import time
import PyQt5
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import QObject, QThread
from PyQt5.QtWidgets import QWidget, QApplication
from Home import Ui_HomeWin # a PyQt5 Designer window
class WorkerThread(QObject):
hmiHandlesThis = PyQt5.QtCore.pyqtSignal(str, str)
def __init__(self, parent=None):
super(WorkerThread, self).__init__(parent)
self.counter = 0
def processRun(self):
while True:
time.sleep(0.5)
print('Doing a bunch of other stuff: {}'.format(self.counter))
self.counter += 1
def update_global_widgets(self):
print('update some global widgets')
def build_a_command(self):
print('building a command...')
name = 'pbAutoMode'
# example command
command = name + '.setStyleSheet("QPushButton{ background-color: rgb(0, 0, 255); }")'
self.hmiHandlesThis.emit(name, command)
class HMI(QWidget):
updateGlobals = PyQt5.QtCore.pyqtSignal(name='updateGlobals')
def __init__(self, parent=None):
super(HMI, self).__init__(parent)
self.HomeWin = QtWidgets.QDialog()
self.HomeWin.setWindowFlags(QtCore.Qt.FramelessWindowHint)
ui = Ui_HomeWin()
ui.setupUi(self.HomeWin)
self.HomeWin.show()
self.EstablishThread()
def EstablishThread(self):
# Create the Object from Class
self.Worker = WorkerThread()
# Assign the Database Signals to Slots
self.Worker.hmiHandlesThis.connect(self.on_new_command)
# Create the Thread
self.ThredHolder = QThread()
# Move the Listener to the Thread
self.Worker.moveToThread(self.ThredHolder)
# Assign the Listener Starting Function to the Thread Call
self.ThredHolder.started.connect(self.Worker.processRun)
# Start the Thread which launches Listener.Connect( )
self.ThredHolder.start()
def on_new_command(self):
print('Handling new command...')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = HMI()
sys.exit(app.exec_())
Thanks Dennis!

Change the shape of a QLabel

I would like a little help, I am trying to generate a chat room but I would like that the QLabel that I use to show the messages had the form of a message container like this
And not only with the typical square shape of a QLabel
I tried to do the following:
def CreateLabel(self):
image = QtGui.QPixmap("container.png")
mask = image.createMaskFromColor(QtCore.Qt.red)
self.Label = QLabel()
self.Label.setText("Test Text")
self.Label.setAlignment(QtCore.Qt.AlignRight)
self.Label.setMask(mask)
Try it:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class Label(QLabel):
def __init__(self, text):
super().__init__()
self.text = text
self.im = QImage('D:/_Qt/img/chat.png')
self.resize(self.im.size())
def paintEvent(self, event):
super().paintEvent(event)
p = QPainter(self)
p.drawImage(0, 0, self.im)
self.drawText(event, p)
def drawText(self, event, p):
p.setPen(QColor(168, 34, 4))
p.setFont(QFont('Decorative', 12))
p.drawText(event.rect(), Qt.AlignTop, self.text)
def closeEvent(self, event):
quit()
class Widget(QWidget):
def __init__(self):
super().__init__()
self.text = """
How to draw a QLabel with chat image.
I would like a little help, I am trying to generate a chat
room but I would like that the QLabel that I use to show
the messages had the form of a message container like this.
"""
self.label = Label(self.text)
self.label.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Widget()
# w.show()
app.exec_()