Problems with the order execution functions when use PyQT4 - python

I have a problem with PyQT4 for Python. There is a label with text and button connected to function. The function could change the text of label first, then call other function. There is a problem with it: the function is executed firts, then change text of the label.
Code:
# -*- coding: utf-8 -*-
import time
import sys
from PyQt4 import QtCore, QtGui
def timesleep():
print("start sleep")
time.sleep(5)
print("stop sleep")
class AnyWidget(QtGui.QWidget):
def __init__(self,*args):
QtGui.QWidget.__init__(self,*args)
self.setWindowTitle("PETHARD")
boxlay = QtGui.QHBoxLayout(self)
frame = QtGui.QFrame(self) # Фрейм
frame.setFrameShape(QtGui.QFrame.StyledPanel)
frame.setFrameShadow(QtGui.QFrame.Raised)
gridlay = QtGui.QGridLayout(frame) # Менеджер размещения элементов во фрейме
label = QtGui.QLabel(u"Welcome",frame) # Текстовая метка.
global glabel
glabel = label
gridlay.addWidget(label,0,0)
button1 = QtGui.QPushButton(u"Load From MC", frame)
self.connect(button1, QtCore.SIGNAL("clicked()"), self.ts)
gridlay.addWidget(button1,1,0)
boxlay.addWidget(frame)
def ts(self):
global glabel
glabel.setText(u"Waiting...")
timesleep()
if __name__=="__main__":
app = QtGui.QApplication(sys.argv)
aw = AnyWidget()
aw.show()
sys.exit(app.exec_())
Help me please to fix this problem.

It work's like that because rendering is done later in app. So your glabel.text is changed immediately but you will see changed text on screen after your ts function call, because drawing is done at the end of loop.
If you really want to call your function in new frame (after rendering of new text) then use timer:
timer = QtCore.QTimer()
QtCore.QObject.connect(
timer,
QtCore.SIGNAL("timeout()"),
self.ts
)
timer.start(10)
It should call your function ten milisecond later, so in fact probably after rendering.

You never want to tie-up your GUI with a long-running function. That the label does not update is only one manifestation of the problem. Even if you get the label to update before the function is called, it will still "freeze" your GUI -- no widget will respond to the user until the long-running function completes.
If you have to run such a function, see if there is a way to break it up into small pieces which each relinquish control to the GUI, or consider using a separate thread to run the function:
import time
import sys
from PyQt4 import QtCore, QtGui
class TimeSleep(QtCore.QThread):
def __init__(self, parent=None):
QtCore.QThread.__init__(self, parent)
self.parent = parent
def run(self):
print("start sleep")
time.sleep(5)
print("stop sleep")
self.parent.glabel.setText(u"Done")
class AnyWidget(QtGui.QWidget):
def __init__(self, *args):
QtGui.QWidget.__init__(self, *args)
self.setWindowTitle("PETHARD")
boxlay = QtGui.QHBoxLayout(self)
frame = QtGui.QFrame(self) # Фрейм
frame.setFrameShape(QtGui.QFrame.StyledPanel)
frame.setFrameShadow(QtGui.QFrame.Raised)
gridlay = QtGui.QGridLayout(
frame) # Менеджер размещения элементов во фрейме
label = QtGui.QLabel(u"Welcome", frame) # Текстовая метка.
self.glabel = label
gridlay.addWidget(label, 0, 0)
button1 = QtGui.QPushButton(u"Load From MC", frame)
self.connect(button1, QtCore.SIGNAL("clicked()"), self.ts)
gridlay.addWidget(button1, 1, 0)
boxlay.addWidget(frame)
def ts(self):
self.glabel.setText(u"Waiting...")
self.thread = TimeSleep(parent=self)
self.thread.start()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
aw = AnyWidget()
aw.show()
sys.exit(app.exec_())
Here is a wiki page on how to deal with long-running functions.

The text of the label is being changed, but because you're blocking the main thread you're not giving Qt a chance to paint it.
Use QCoreApplication::processEvents and QCoreApplication::flush:
def ts(self):
glabel.setText(u"Waiting...")
QtCore.QCoreApplication.processEvents()
QtCore.QCoreApplication.flush()
timesleep()

Related

Break an infinit loop when button is pressed

I am currently starting to use PyQt5 and created my first GUI. Now I would like a program that does the following. When Button 'start' is pressed execute 'function' n-times until Button 'stop' is pressed (imagine a stopwatch) if I press Button 'start' again 'function' gets once again executed and so on.
What I tried so far is the following, that can not work, since we are not able to change a variable from outside the loop (mockup-script)
class Ui(QtWidgets.QDialog):
def __init__(self):
super(Ui, self).__init__()
uic.loadUi('interfaceScan.ui', self)
self.startScan = self.findChild(QtWidgets.QPushButton, 'startScan')
self.startScan.clicked.connect(self.startPressed)
self.pauseScan = self.findChild(QtWidgets.QPushButton, 'pauseScan')
self.pauseScan.clicked.connect(self.pausePressed)
self.show()
def startPressed(self):
global pauseScan
pauseScan = False
dosomething()
def pausePressed(self):
global pauseScan
pausScan = True
def dosomething():
while pauseScan == False: #not needed, but the measurement should be executed periodically until 'pause' is pressed
print('Running') #in the reals program the measurement will be executed here
time.sleep(4) #between each measurement the program needs to wait a cirtain amount of time ~1h
app = QtWidgets.QApplication(sys.argv)
window = Ui()
app.exec_()
Any ideas on how I can solve this problem? I am now relatively certain that it does not work when using a while loop, so I am open to suggestions on how to change it up!
The purpose of this script will be to control a measurement setup that should run for let's say 1000 cycles, but I would like to be able to break it in between to change so parameters.
As the OP points out in the comments, the while loop with the time.sleep() only aims to perform a periodic task but this generates a problem since the time.sleep() freezes the GUI, instead a QTimer must be used:
from PyQt5 import QtCore, QtWidgets, uic
class Ui(QtWidgets.QDialog):
def __init__(self):
super(Ui, self).__init__()
uic.loadUi("interfaceScan.ui", self)
self.startScan.clicked.connect(self.startPressed)
self.pauseScan.clicked.connect(self.pausePressed)
self.timer = QtCore.QTimer(self, interval=4 * 1000, timeout=dosomething)
#QtCore.pyqtSlot()
def startPressed(self):
QtCore.QTimer.singleShot(0, dosomething)
self.timer.start()
#QtCore.pyqtSlot()
def pausePressed(self):
self.timer.stop()
def dosomething():
print("Running")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = Ui()
window.show()
sys.exit(app.exec_())

PyQt4 QMessageBox text change

I am using this simple code to show warning boxes:
w = QWidget()
result = QMessageBox.warning(w, 'a', x, QMessageBox.Ok)
Is there any way to change MESSAGE dynamically? I want to make a popup which will inform user abut progress of a task that is running in background.
Edit:
Well I tried to do so making this script for testing:
def handleButton(self):
self.msgBox = QMessageBox(self)
self.msgBox.setWindowTitle("Title")
self.msgBox.setIcon(QMessageBox.Warning)
self.msgBox.setText("Start")
self.msgBox.show()
x = 0
for x in range (100):
x = x + 1
print (x)
self.msgBox.setText(str(x))
self.msgBox.show()
time.sleep(1)
The text only shows after finishing the 'for loop', why?
Instead of using a static method you could create an object of the class.
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Widget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.msgBox = QMessageBox(self)
self.msgBox.setWindowTitle("Title")
self.msgBox.setIcon(QMessageBox.Warning)
self.msgBox.setText("Start")
self.msgBox.show()
timer = QTimer(self)
timer.timeout.connect(self.onTimeout)
timer.start(1000)
def onTimeout(self):
self.msgBox.setText("datetime: {}".format(QDateTime.currentDateTime().toString()))
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
Update:
The problem in your example is the use of time.sleep(). Qt is executed in an eventloop, this eventloop allows you to handle the events of the mouse, keyboard, redraw, etc. but the time.sleep() blocks the eventloop, this you can check trying to change the size of the window, you will see that you can not do it.
Assuming you use time.sleep() to pause, then you must use QEventLoop with QTimer that does not block the Qt eventloop.
def handleButton(self):
self.msgBox = QMessageBox(self)
self.msgBox.setWindowTitle("Title")
self.msgBox.setIcon(QMessageBox.Warning)
self.msgBox.setText("Start")
self.msgBox.show()
for x in range(100):
self.msgBox.setText(str(x+1))
loop = QEventLoop()
QTimer.singleShot(1000, loop.quit)
loop.exec_()

PyQt5: Update labels inrun time

I have a problem updating labels in a loop during run time. I think I need to use signals and so on, but I've tried everything I can think of now. What I want my program to do:
When I click the button a loop should start that run some function that take some time. While the function is running the corresponding label should update its text to say "running" and when its done it should say "done" and continue to the next function. I've created some dummy code that represent what I'm after!
I have represented my function with a dummy function that just take some time.
import sys
from PyQt5.QtWidgets import *
import time
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# Set up an example ui
qbtn = QPushButton('Click', self)
qbtn.clicked.connect(self.changeLabels)
qbtn.resize(qbtn.sizeHint())
qbtn.move(100, 50)
# Add labels
self.labels = list()
self.labels.append(QLabel("Lbl1", self))
self.labels[-1].setFixedSize(50, 20)
self.labels.append(QLabel("Lbl2", self))
self.labels[-1].setFixedSize(50, 20)
self.labels[-1].move(0, 20)
self.labels.append(QLabel("Lbl3", self))
self.labels[-1].setFixedSize(50, 20)
self.labels[-1].move(0, 40)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Test')
self.show()
def changeLabels(self):
# Loop over all labels. For each label a function will be executed that will take some time. In this case
# I represent that with a dummy function to just take time. While the function is running the label should say
# "running" and when its finished it should say "done".
for lbl in self.labels:
orgTxt = lbl.text()
lbl.setText("%s Running" % orgTxt)
self.dummyFunction()
lbl.setText("%s Done" % orgTxt)
def dummyFunction(self):
time.sleep(1)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
The GUI does not support blocking tasks because the GUI needs some time to update some attributes of the window, a possible solution is to use a new thread and implement the dummy function, in the following example the implementation is shown with the use of signals. If your real function updates any GUI view you should not do it directly in the thread, you should do it through signals.
class DummyThread(QThread):
finished = pyqtSignal()
def run(self):
time.sleep(1)
self.finished.emit()
class Example(QWidget):
[...]
def changeLabels(self):
for lbl in self.labels:
orgTxt = lbl.text()
lbl.setText("%s Running" % orgTxt)
thread = DummyThread(self)
thread.start()
thread.finished.connect(lambda txt=orgTxt, lbl=lbl : lbl.setText("%s Done" % txt))
An easier way would be to call QApplication.processEvents() to force QT to process all the events present on the queue. But the UI won't be as responsive as it would be with another thread doing the work and sending signals to the main thread as stated in other answers.

PyQt4: use a QTimer to continually update progress bars

I have a simple dialog with three progress bars that I want to continually update (displaying system resource usage). From reading around the docs, QTimer is the right way to fire a function every x milliseconds (which would update the progress bars). However, I am not able to get it to work and I don't quite know why. It seems relatively simple to connect up the timer timeout signal to an update function, but it never seems to fire.
Here's my code:
import sys
from PyQt4 import QtGui, QtCore
import psutil
class Tiny_System_Monitor(QtGui.QWidget):
def __init__(self):
super(Tiny_System_Monitor, self).__init__()
self.initUI()
def initUI(self):
mainLayout = QtGui.QHBoxLayout()
self.cpu_progressBar = QtGui.QProgressBar()
self.cpu_progressBar.setTextVisible(False)
self.cpu_progressBar.setOrientation(QtCore.Qt.Vertical)
mainLayout.addWidget(self.cpu_progressBar)
self.vm_progressBar = QtGui.QProgressBar()
self.vm_progressBar.setOrientation(QtCore.Qt.Vertical)
mainLayout.addWidget(self.vm_progressBar)
self.swap_progressBar = QtGui.QProgressBar()
self.swap_progressBar.setOrientation(QtCore.Qt.Vertical)
mainLayout.addWidget(self.swap_progressBar)
self.setLayout(mainLayout)
timer = QtCore.QTimer()
timer.timeout.connect(self.updateMeters)
timer.start(1000)
def updateMeters(self):
cpuPercent = psutil.cpu_percent()
vmPercent = getattr(psutil.virtual_memory(), "percent")
swapPercent = getattr(psutil.swap_memory(), "percent")
self.cpu_progressBar.setValue(cpuPercent)
self.vm_progressBar.setValue(vmPercent)
self.swap_progressBar.setValue(swapPercent)
print "updated meters"
def main():
app = QtGui.QApplication(sys.argv)
ex = Tiny_System_Monitor()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You must keep a reference to the timer object, otherwise it will be immediately garbage-collected when initUI returns:
class Tiny_System_Monitor(QtGui.QWidget):
...
def initUI(self):
...
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.updateMeters)
self.timer.start(1000)

python UI freezing

I'm trying to make basic functionality
after pressing "start" button start counter , after pressing stop button stop counter,
but after I start process, it looks like only counting thread is working and it's not possible to press stop button
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore
from test.test_sax import start
import time
from threading import Thread
import threading
class Example(QtGui.QWidget):
x = 1
bol = True
def __init__(self):
super(Example, self).__init__()
self.qbtn = QtGui.QPushButton('Quit', self)
self.qbtn.resize(self.qbtn.sizeHint())
self.qbtn.move(50, 50)
self.qbtn2 = QtGui.QPushButton('Start', self)
self.qbtn2.resize(self.qbtn2.sizeHint())
self.qbtn2.move(150, 50)
self.qbtn.clicked.connect(self.stopCounter)
self.qbtn2.clicked.connect(self.startUI)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Quit button')
self.show()
def stopCounter(self):
Example.bol = False
def startUI(self):
Example.bol = True
thread = Thread(self.counterr())
def counterr(self):
x = 0
while Example.bol:
print x
x += 1
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
a = Example()
sys.exit(app.exec_())
thx
Now you call the slow function before you even create the thread. Try this instead:
thread = Thread(target=self.counterr)
thread.start()
In a Qt application you might also consider the QThread class that can run its own event loop and communicate with your main thread using signals and slots.
You are using the Thread class completely incorrectly, I'm afraid. You are passing it the result of the counterr method, which never returns.
Pass counterr (without calling it) to the Thread class as the target, then start it explicitly:
def startUI(self):
self.bol = True
thread = Thread(target=self.counterr)
thread.start()
Also, just access bol as an instance variable, not a class variable.

Categories