How to count lines in qscintilla? - pyqt5

I'm using QScintilla to make my own notepad for fun in pyqt5 python. I was wandering if there is a way to get the number of lines of a QScintilla() widget?

You have to use the lines() method, you can also use the linesChanged signal.
import sys
from PyQt5 import QtWidgets, Qsci
class Editor(Qsci.QsciScintilla):
def __init__(self, parent=None):
super().__init__(parent)
self.setText("Foo\nBar")
self.print_lines()
self.linesChanged.connect(self.handle_lines_changed)
def handle_lines_changed(self):
self.print_lines()
def print_lines(self):
print("total lines: {}".format(self.lines()))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = Editor()
w.resize(640, 480)
w.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_())

Qpushbutton in custom QlistWidgetItem doesn't respond when running on embedded device

I'm quite new to pyqt5 and applications on embedded devices, and was hoping someone may have any tips or suggestions on the problems I'm facing.
I am working with a pyqt5 appliaction where the widget contains a QListWidget and two buttons. I have made a seperate widget that I use as a customized QListWidgetItem. The customized QListWidgetItem contains a textfield and a QPushButton.
Everything works perfectly on Ubuntu on my computer, but when I deploy and run the app on an embedded device, the buttons in the customized QListWidgetItem no longer responds. The "normal" buttons, that are not in the customized QListWidgetItem, still works normally.
I can't find a reason or fixes for this, and am quite stuck..
Here's my code, very simplified:
Edit! After some feedback I've created a smaller representation of the app/problem, which hopefully can make it easier to debug.
I no longer use pyqtSignal between the classes, only connected the buttons to different functions directly.
In this case, both the printing and color changing will happen if the buttons respond correctly.
python file:
import sys
from CustomListItem import Ui_custom_list_widget
from CollectorWidget import Ui_collector_widget
from PyQt5.QtWidgets import QWidget, QListWidgetItem, QApplication, QStackedWidget, QMainWindow
def main():
app = QApplication(sys.argv)
stack = QStackedWidget()
collector = CollectorWidget()
stack.addWidget(collector)
stack.setCurrentWidget(collector)
stack.show()
sys.exit(app.exec_())
class CollectorWidget(Ui_collector_widget, QWidget):
temp_list_items = []
def __init__(self, parent=None):
super(CollectorWidget, self).__init__(parent)
self.setupUi(self)
self.create_item_button.clicked.connect(self.create_item_clicked)
self.another_button.clicked.connect(lambda: print("this one works"))
def create_item_clicked(self):
self.listItem = QListWidgetItem()
self.item = CustomListItem()
self.item.custom_item_button.clicked.connect(self.test_signal)
self.qlistwidget_list.addItem(self.listItem)
self.qlistwidget_list.setItemWidget(self.listItem, self.item)
self.temp_list_items.append((self.listItem, self.item))
def test_signal(self):
print("--------------IT'S WORKING!--------------------------")
class CustomListItem(Ui_custom_list_widget, QWidget):
def __init__(self):
super().__init__()
self.setupUi(self)
self.item_name.setText("testing testing")
self.custom_item_button.clicked.connect(self.button_clicked)
def setItemText(self, text):
self.item_name.setText(text)
def button_clicked(self):
self.item_name.setStyleSheet("background-color: red;")
if __name__ == "__main__":
main()
Ui files (collector):
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'CollectorWidget.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_collector_widget(object):
def setupUi(self, collector_widget):
collector_widget.setObjectName("collector_widget")
collector_widget.resize(625, 514)
self.verticalLayout = QtWidgets.QVBoxLayout(collector_widget)
self.verticalLayout.setObjectName("verticalLayout")
self.qlistwidget_list = QtWidgets.QListWidget(collector_widget)
self.qlistwidget_list.setObjectName("qlistwidget_list")
self.verticalLayout.addWidget(self.qlistwidget_list)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.create_item_button = QtWidgets.QPushButton(collector_widget)
self.create_item_button.setObjectName("create_item_button")
self.horizontalLayout.addWidget(self.create_item_button)
self.another_button = QtWidgets.QPushButton(collector_widget)
self.another_button.setObjectName("another_button")
self.horizontalLayout.addWidget(self.another_button)
self.verticalLayout.addLayout(self.horizontalLayout)
self.retranslateUi(collector_widget)
QtCore.QMetaObject.connectSlotsByName(collector_widget)
def retranslateUi(self, collector_widget):
_translate = QtCore.QCoreApplication.translate
collector_widget.setWindowTitle(_translate("collector_widget", "Form"))
self.create_item_button.setText(_translate("collector_widget", "Create"))
self.another_button.setText(_translate("collector_widget", "Just a button"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
collector_widget = QtWidgets.QWidget()
ui = Ui_collector_widget()
ui.setupUi(collector_widget)
collector_widget.show()
sys.exit(app.exec_())
Ui files (custom_list_item):
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'CustomListItem.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_custom_list_widget(object):
def setupUi(self, custom_list_widget):
custom_list_widget.setObjectName("custom_list_widget")
custom_list_widget.resize(608, 40)
self.horizontalLayout = QtWidgets.QHBoxLayout(custom_list_widget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setSpacing(0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.item_name = QtWidgets.QLabel(custom_list_widget)
font = QtGui.QFont()
font.setKerning(True)
self.item_name.setFont(font)
self.item_name.setStyleSheet("")
self.item_name.setWordWrap(True)
self.item_name.setObjectName("item_name")
self.horizontalLayout_2.addWidget(self.item_name)
self.custom_item_button = QtWidgets.QPushButton(custom_list_widget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.custom_item_button.sizePolicy().hasHeightForWidth())
self.custom_item_button.setSizePolicy(sizePolicy)
self.custom_item_button.setMaximumSize(QtCore.QSize(20, 20))
self.custom_item_button.setStyleSheet("")
self.custom_item_button.setText("")
self.custom_item_button.setObjectName("custom_item_button")
self.horizontalLayout_2.addWidget(self.custom_item_button)
self.horizontalLayout.addLayout(self.horizontalLayout_2)
self.retranslateUi(custom_list_widget)
QtCore.QMetaObject.connectSlotsByName(custom_list_widget)
def retranslateUi(self, custom_list_widget):
_translate = QtCore.QCoreApplication.translate
custom_list_widget.setWindowTitle(_translate("custom_list_widget", "Form"))
self.item_name.setText(_translate("custom_list_widget", "TextLabel"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
custom_list_widget = QtWidgets.QWidget()
ui = Ui_custom_list_widget()
ui.setupUi(custom_list_widget)
custom_list_widget.show()
sys.exit(app.exec_())
Picture of app:

Calling method from other class

I'm very new to PyQt5, and I'm trying to make an interactive gui for plotting of data. However, this problem may well be completely unrelated to PyQt5 and more a problem with my understanding of object oriented programming in general.
I have a MainWindow class, a SupportClass1 and a SupportClass2. When I make an instance of SupportClass1, I want to call the method DoSomething in the MainWindow class by referring to the object window, but I get the error message NameError: name 'window' is not defined.
I have no problems creating a method in the SupportClass2 and calling that from the MainWindow class so I get the impression that I have not instantiated the MainWindow class correctly which I don't understand as I thought I had defined window as an instace of the MainWindow class.
Can anyone help me understand what is wrong in my logic and how to solve this problem?
from PyQt5 import QtWidgets, QtCore
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
import sys
import os
from random import randint
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.graphWidget = pg.PlotWidget()
self.x = list(range(100))
self.y = [randint(0,100) for _ in range(100)]
self.graphWidget.setBackground('w')
pen = pg.mkPen(color=(255, 0, 0))
self.data_line = self.graphWidget.plot(self.x, self.y, pen=pen)
self.button = QPushButton('Test')
self.button.clicked.connect(self.InstantiateSupportClasses)
self.gui_box = QVBoxLayout()
self.gui_box.addWidget(self.graphWidget)
self.gui_box.addWidget(self.button)
self.setLayout(self.gui_box)
self.setGeometry(300, 300, 300, 150)
self.setWindowTitle('Test application')
self.show()
def InstantiateSupportClasses(self):
supp_class2 = SupportClass2()
print(supp_class2.GetVariable())
supp_class1 = SupportClass1()
def DoSomething(self):
print('I did something!')
class SupportClass1():
def __init__(self):
window.DoSomething
class SupportClass2():
def __init__(self):
self.some_variable = 5
def GetVariable(self):
return self.some_variable
def main():
app = QApplication(sys.argv)
app.setStyle('Fusion')
window = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()```
I see that you are using the "window" object from the "SupportClass1" class.
but that class does not recognize this object one solution is to insert that object to the "SupportClass1()"
from PyQt5 import QtWidgets, QtCore
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
import sys
import os
from random import randint
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.graphWidget = pg.PlotWidget()
self.x = list(range(100))
self.y = [randint(0,100) for _ in range(100)]
self.graphWidget.setBackground('w')
pen = pg.mkPen(color=(255, 0, 0))
self.data_line = self.graphWidget.plot(self.x, self.y, pen=pen)
self.button = QPushButton('Test')
self.button.clicked.connect(self.InstantiateSupportClasses)
self.gui_box = QVBoxLayout()
self.gui_box.addWidget(self.graphWidget)
self.gui_box.addWidget(self.button)
self.setLayout(self.gui_box)
self.setGeometry(300, 300, 300, 150)
self.setWindowTitle('Test application')
self.show()
def InstantiateSupportClasses(self):
supp_class2 = SupportClass2()
print(supp_class2.GetVariable())
supp_class1 = SupportClass1(self)
def DoSomething(self):
print('I did something!')
class SupportClass1():
def __init__(self, window):
window.DoSomething()
class SupportClass2():
def __init__(self):
self.some_variable = 5
def GetVariable(self):
return self.some_variable
def main():
app = QApplication(sys.argv)
app.setStyle('Fusion')
window = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

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!

pyqt5 videowidget not showing in layout

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