I'm trying to repeatedly call a decorated function from within another class, such that the decorator is executed everytime the function is called.
The original question is below but does not explicitly relate to pyqt as pointed out correctly.
I'm trying to use decorators within a pyqt thread. From how I understand decorators, the decoration should be executed every time the function is called. (Or at least that is what I want.) However, calling a decorated function from within a pyqt thread leads to execution of the decorator only once.
This is my tested example:
import time, sys
import numpy as np
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Decor:
def deco(self, *args, **kwargs):
print(kwargs['txt'])
print("In decorator")
def inner(func):
return func
return inner
dec = Decor()
class Window(QWidget):
def __init__(self, parent = None):
QWidget.__init__(self, parent)
self.thread = Worker()
label = QLabel(self.tr("random number"))
self.thread.output[str].connect(label.setText)
layout = QGridLayout()
layout.addWidget(label, 0, 0)
self.setLayout(layout)
self.thread.start()
class Worker(QThread):
output = pyqtSignal(str)
def run(self):
# Note: This is never called directly. It is called by Qt once the
# thread environment has been set up.
while True:
time.sleep(1)
number = self.random()
self.output.emit('random number {}'.format(number))
#dec.deco(txt='kw_argument')
def random(self):
return np.random.rand(1)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
I expected to get the prints of 'kw_argument' and 'in decorator' as often as self.normal()is called, but get it only once. What am I doing wrong?
You could use function decorator instead:
import time, sys
from functools import wraps
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
def dec2(*args, **kwargs):
def real_decorator(fn):
#wraps(fn)
def wrapper(*args, **kwargs):
print(args, kwargs)
print("In decorator")
return fn(*args, **kwargs)
return wrapper
return real_decorator
class Window(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.thread = Worker()
label = QLabel(self.tr("random number"))
self.thread.output[str].connect(label.setText)
layout = QGridLayout()
layout.addWidget(label, 0, 0)
self.setLayout(layout)
self.thread.start()
class Worker(QThread):
output = pyqtSignal(str)
def run(self):
# Note: This is never called directly. It is called by Qt once the
# thread environment has been set up.
while True:
time.sleep(1)
number = self.random()
self.output.emit('random number {}'.format(number))
#dec2(txt='kw_argument')
def random(self):
return np.random.rand(1)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
Out:
(<__main__.Worker object at 0x10edc37d0>,) {}
In decorator
(<__main__.Worker object at 0x10edc37d0>,) {}
In decorator
(<__main__.Worker object at 0x10edc37d0>,) {}
In decorator
(<__main__.Worker object at 0x10edc37d0>,) {}
In decorator
(<__main__.Worker object at 0x10edc37d0>,) {}
In decorator
...
If you really need to print txt always, stick with a class decorator:
class dec2(object):
def __init__(self, *args, **kwargs):
self.deco_args = args
self.deco_kwargs = kwargs
def __call__(self, f):
def wrapped_f(*args):
print(self.deco_kwargs['txt'])
print('in decorator')
return f(*args)
return wrapped_f
Out:
w_argument
in decorator
kw_argument
in decorator
...
Related
I want to use multiple imported function with arguments that takes some while to run. I want a 'working' progress bar that track the processes of that function. I have followed 2 questions already here.
Connect an imported function to Qt5 progress bar without dependencies
Report progress to QProgressBar using variable from an imported module
The difference is that the thread can take any function which can have arguments. The function also not needs to yield the percent to return to the progressbar. The progressbar always start at 0%.
I copied a snippet from first link and modified it for example purpose.
from external_script import long_running_function
class Actions(QDialog):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Progress Bar')
self.progress = QProgressBar(self)
self.button = QPushButton('Start', self)
self.show()
self.button.clicked.connect(self.onButtonClick)
def onButtonClick(self):
long_running_function(**kwargs) # This can be any function that takes argument/s
self.progress.setValue(value)
Do not get too complicated with the answers as they are limited to a very particular context. In general the logic is to pass a QObject to it that updates the percentage value and then emits a signal with that value. For example a simple solution is to use the threading module:
import sys
import threading
from PyQt5 import QtCore, QtWidgets
class PercentageWorker(QtCore.QObject):
started = QtCore.pyqtSignal()
finished = QtCore.pyqtSignal()
percentageChanged = QtCore.pyqtSignal(int)
def __init__(self, parent=None):
super().__init__(parent)
self._percentage = 0
#property
def percentage(self):
return self._percentage
#percentage.setter
def percentage(self, value):
if self._percentage == value:
return
self._percentage = value
self.percentageChanged.emit(self.percentage)
def start(self):
self.started.emit()
def finish(self):
self.finished.emit()
class FakeWorker:
def start(self):
pass
def finish(self):
pass
#property
def percentage(self):
return 0
#percentage.setter
def percentage(self, value):
pass
import time
def long_running_function(foo, baz="1", worker=None):
if worker is None:
worker = FakeWorker()
worker.start()
while worker.percentage < 100:
worker.percentage += 1
print(foo, baz)
time.sleep(1)
worker.finish()
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.progress = QtWidgets.QProgressBar()
self.button = QtWidgets.QPushButton("Start")
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.button)
lay.addWidget(self.progress)
self.button.clicked.connect(self.launch)
def launch(self):
worker = PercentageWorker()
worker.percentageChanged.connect(self.progress.setValue)
threading.Thread(
target=long_running_function,
args=("foo",),
kwargs=dict(baz="baz", worker=worker),
daemon=True,
).start()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
When I click the minimize button to perform this operation in the background,Threads often execute beyond the limited time,
Waking up the program in the background is back to normal.
Please be patient, it will appear in about a minute.
import sys, random, time, functools
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMainWindow, QHBoxLayout
from PyQt5.QtCore import QThread, QObject
def clock(func):
#functools.wraps(func)
def clocked(*args, **kwargs):
t0 = time.time()
result = func(*args, **kwargs)
elapsed = time.time() - t0
name = func.__name__
l_arg = []
if args:
l_arg.append(', '.join(repr(arg) for arg in args))
arg_str = ', '.join(l_arg)
print('[%0.5fs] %s(%s)' % (elapsed, name, arg_str))
return result
return clocked
#clock
def go_sleep(sleep_time):
time.sleep(sleep_time)
def go_run():
for i in range(100):
go_sleep(random.randint(1, 3))
class WorkThread(QObject):
def __int__(self):
super(WorkThread, self).__init__()
def run(self):
go_run()
class WinForm(QMainWindow):
def __init__(self, parent=None):
super(WinForm, self).__init__(parent)
self.button1 = QPushButton('Run')
self.button1.clicked.connect(self.onButtonClick)
self._thread = QThread(self)
self.wt = WorkThread()
self.wt.moveToThread(self._thread)
layout = QHBoxLayout()
layout.addWidget(self.button1)
main_frame = QWidget()
main_frame.setLayout(layout)
self.setCentralWidget(main_frame)
def onButtonClick(self):
self.button1.setText('Running')
self._thread.started.connect(self.wt.run)
self._thread.start()
if __name__ == "__main__":
app = QApplication(sys.argv)
form = WinForm()
form.show()
sys.exit(app.exec_())
turn App Nap back off
defaults write NSGlobalDomain NSAppSleepDisabled -bool YES
Using PyQt5, there are certain event handlers which I can bind to my object, and others that only work if I implement them as methods. changeEvent and event are examples of the later type.
You can see in my example below that I can add a keyPressEvent handler to my widget programmatically, but I can not do the same thing for changeEvent.
from PyQt5 import QtGui, QtWidgets, QtCore
import types
def keyPressEvent(self, key: QtGui.QKeyEvent) -> None:
#works
print(key.isAutoRepeat())
def changeEvent(self, a0: QtCore.QEvent) -> None:
#doesn't work
print("bound change event", a0.type())
bindable = [keyPressEvent, changeEvent]
def bind_key_functions(target):
for bound in bindable:
setattr(target, bound.__name__, types.MethodType(bound, target))
class my_widget(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle("w1")
bind_key_functions(self)
class my_widget2(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle("w2")
def changeEvent(self, a0: QtCore.QEvent) -> None:
#this does work
print("derived change event", a0.type())
app = QtWidgets.QApplication([])
mw1 = my_widget()
mw1.show()
mw2 = my_widget2()
mw2.show()
app.exec_()
What makes the changeEvent different? how can I force it to behave as I want?
Using setattr to override methods is a bad choice as it is not very elegant and if you want to listen to the events of another QWidget then it is better to use an event filter.
from PyQt5 import QtGui, QtWidgets, QtCore
class Binder(QtCore.QObject):
def __init__(self, qobject):
super().__init__(qobject)
self._qobject = qobject
self.qobject.installEventFilter(self)
#property
def qobject(self):
return self._qobject
def eventFilter(self, obj, event):
if self.qobject is obj:
print(event.type(), event)
return super().eventFilter(obj, event)
class My_Widget(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle("w1")
Binder(self)
class My_Widget2(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle("w2")
app = QtWidgets.QApplication([])
mw1 = My_Widget()
mw1.show()
mw2 = My_Widget2()
mw2.show()
app.exec_()
On the other hand, it is not documented which methods can be assigned or not, so if you want to find the reason you must analyze the source code of sip and pyqt5. What little is pointed out is that PyQt5 creates a cache of the methods (it is not known when or what methods are stored in the cache).
I am trying to emit custom events in PyQt. One widget would emit and another would listen to events, but the two widgets would not need to be related.
In JavaScript, I would achieve this by doing
// Component 1
document.addEventListener('Hello', () => console.log('Got it'))
// Component 2
document.dispatchEvent(new Event("Hello"))
Edit: I know about signals and slots, but only know how to use them between parent and child. How would I this mechanism (or other mechanism) between arbitrary unrelated widgets?
In PyQt the following instruction:
document.addEventListener('Hello', () => console.log('Got it'))
is equivalent
document.hello_signal.connect(lambda: print('Got it'))
In a similar way:
document.dispatchEvent(new Event("Hello"))
is equivalent
document.hello_signal.emit()
But the big difference is the scope of the "document" object, since the connection is between a global element. But in PyQt that element does not exist.
One way to emulate the behavior that you point out is by creating a global object:
globalobject.py
from PyQt5 import QtCore
import functools
#functools.lru_cache()
class GlobalObject(QtCore.QObject):
def __init__(self):
super().__init__()
self._events = {}
def addEventListener(self, name, func):
if name not in self._events:
self._events[name] = [func]
else:
self._events[name].append(func)
def dispatchEvent(self, name):
functions = self._events.get(name, [])
for func in functions:
QtCore.QTimer.singleShot(0, func)
main.py
from PyQt5 import QtCore, QtWidgets
from globalobject import GlobalObject
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
button = QtWidgets.QPushButton(text="Press me", clicked=self.on_clicked)
self.setCentralWidget(button)
#QtCore.pyqtSlot()
def on_clicked(self):
GlobalObject().dispatchEvent("hello")
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
GlobalObject().addEventListener("hello", self.foo)
self._label = QtWidgets.QLabel()
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self._label)
#QtCore.pyqtSlot()
def foo(self):
self._label.setText("foo")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w1 = MainWindow()
w2 = Widget()
w1.show()
w2.show()
sys.exit(app.exec_())
I'm writing an UI using QT5 and python, I added a thread to handle the UI, thread works "fine", a function inside the thread receive 2 strings and return 2 strings (I'm making experiments before develop the real project just to see how it works), as you can see in the code after call the thread function with:
self.requestConexion.emit('lblText1','dddddd')
I call another function that is just a simple counter
self.contador()
so I expect that before the counter finish the value of the control self.lblText1 change, but this is not happen... here is the main code:
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import UI_test
import time
import sys
class Threaded(QObject):
result=pyqtSignal(str,str)
def __init__(self, parent=None, **kwargs):
super().__init__(parent, **kwargs)
#pyqtSlot(str,str)
def etiquetas(self,lbl,texto):
print(texto)
self.result.emit(lbl,texto)
class MainApp(QMainWindow, UI_test.Ui_MainWindow):
requestConexion=pyqtSignal(str,str)
def __init__(self, parent=None):
super(MainApp, self).__init__(parent)
self._thread=QThread()
self._threaded=Threaded(result=self.displayLabel)
self.requestConexion.connect(self._threaded.etiquetas)
self._threaded.moveToThread(self._thread)
qApp.aboutToQuit.connect(self._thread.quit)
self._thread.start()
self.setupUi(self)
self.btnStart.clicked.connect(self.conexion)
#pyqtSlot()
def conexion(self):
#self._thread.start()
print(1)
self.requestConexion.emit('lblText1','dddddd')
self.contador()
text, ok = QInputDialog.getText(self, 'Text Input Dialog', 'Enter your name:')
if ok:
print(str(text))
#pyqtSlot()
def contador(self):
i=0
while i<50:
print(i)
time.sleep(0.1)
i+=1
#pyqtSlot(str,str)
def displayLabel(self, etiqueta, texto):
self.lblText1.setText(etiqueta)
print(texto)
def main():
app = QApplication(sys.argv)
form = MainApp()
form.show()
app.exec_()
exit(app.exec_())
if __name__ == '__main__':
main()
any idea whats wrong?
I finally find the answer to my question in the next blog:
https://martinfitzpatrick.name/article/multithreading-pyqt-applications-with-qthreadpool/
this is a really great tutorial, after read the documentation in the blog I was able to modify one of the examples to modify the text of a label control in real time; here is the final code:
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import time
class Worker(QRunnable):
def __init__(self, fn, *args, **kwargs):
super(Worker, self).__init__()
self.args = args
self.kwargs = kwargs
self.fn = fn
#pyqtSlot()
def run(self):
#print(self.args, self.kwargs)
print("Thread start")
time.sleep(0.2)
self.fn(*self.args, **self.kwargs) #ejecuta la funcion recibida
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.threadpool = QThreadPool()
print("Multithreading with maximum %d threads" % self.threadpool.maxThreadCount())
self.counter = 0
layout = QVBoxLayout()
self.l = QLabel("Start")
self.l2 = QLabel("xxxxx")
b = QPushButton("DANGER!")
b.pressed.connect(self.oh_no)
layout.addWidget(self.l)
layout.addWidget(self.l2)
layout.addWidget(b)
w = QWidget()
w.setLayout(layout)
self.setCentralWidget(w)
self.show()
self.etiqueta="rrrrrrrrrr"
self.timer = QTimer()
self.timer.setInterval(500)
self.timer.timeout.connect(self.recurring_timer)
self.timer.start()
def oh_no(self):
self.etiqueta="hhhhhhhhhhhhhhhhh"
worker = Worker(self.execute_this_fn,'4444')
self.threadpool.start(worker)
def recurring_timer(self):
self.counter +=1
self.l.setText("Counter: %d" % self.counter)
def execute_this_fn(self,x):
print("Hello!")
self.l2.setText(x)
app = QApplication([])
window = MainWindow()
app.exec_()