change parameters during sounddevice processing in Pyqt dial - python

I want to turn the dial on pyqt's GUI to change parameters while sounddevice's outputstream is processing, but the GUI freezes. I've tried everything I could find, and I've pieced together a bunch of code, but I'd like to know what the solution is.
I want to change the equalizer of the music being played in near real time by changing the parameters of the pedalboard that grants the equalizer.
import sys
import time
from PyQt5.QtCore import QObject, QThread, pyqtSignal
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import QSound
from pedalboard import *
import sounddevice as sd
import librosa
from threading import *
import threading
import soundfile as sf
def long_running_function(update_ui,board):
event = threading.Event()
try:
data, fs = sf.read('continue.wav', always_2d=True)
current_frame = 0
def callback(outdata, frames, time, status):
nonlocal current_frame
if status:
print(status)
chunksize = 2024
#print(chunksize, current_frame,len(data),frames)
outdata[:chunksize] = board(data[current_frame:current_frame + chunksize])
if chunksize < 2024:
outdata[chunksize:] = 0
raise sd.CallbackStop()
current_frame += chunksize
stream = sd.OutputStream(
samplerate=fs, blocksize=2024 , channels=data.shape[1],
callback=callback, finished_callback=event.set)
with stream:
event.wait()
except KeyboardInterrupt:
exit('\nInterrupted by user')
class Worker(QObject):
finished = pyqtSignal()
progress = pyqtSignal(int)
def __init__(self, main_window):
self.main_window = main_window
super(Worker, self).__init__(main_window)
def run(self):
long_running_function(self.update_progress,self.main_window.make_board())
self.finished.emit()
def update_progress(self, percent):
self.progress.emit(percent)
class MainWindow(QMainWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.progress = QProgressBar()
self.button = QPushButton("Start")
self.dial = QDial()
self.init_ui()
self.qsound = None
self.qsound2 = None
self.effected_audio = None
self.audio = None
self.sr = None
self.dial.valueChanged.connect(self.make_board)
self.button.clicked.connect(self.execute)
self.show()
def init_ui(self):
self.setGeometry(100, 100, 250, 250)
layout = QVBoxLayout()
layout.addWidget(self.progress)
layout.addWidget(self.button)
layout.addWidget(self.dial)
self.setWindowTitle('Audio Player')
button_play = QPushButton("環境音を再生")
button_play2 = QPushButton("音楽と環境音を再生")
button_stop = QPushButton("Stop")
button_dialog = QPushButton("音楽を選択")
button_dialog2 = QPushButton("環境音を選択")
dial = QDial()
live = QPushButton("LIVE")
self.label = QLabel(self)
self.label2 = QLabel(self)
layout.addWidget(button_dialog)
layout.addWidget(button_dialog2)
layout.addWidget(button_play)
layout.addWidget(button_stop)
layout.addWidget(button_play2)
layout.addWidget(live)
layout.addWidget(self.dial)
layout.addWidget(self.label)
layout.addWidget(self.label2)
button_dialog.clicked.connect(self.button_openfile)
button_dialog2.clicked.connect(self.button_openfile2)
button_play.clicked.connect(self.button_play)
button_play2.clicked.connect(self.button_play2)
button_stop.clicked.connect(self.button_stop)
live.clicked.connect(self.start)
self.dial.valueChanged.connect(self.sliderMoved)
self.dial.valueChanged.connect(self.make_board)
self.dial.setMinimum(0)
self.dial.setMaximum(20)
self.dial.setValue(5)
w = QWidget()
w.setLayout(layout)
self.setCentralWidget(w)
def sliderMoved(self):
number = self.dial.value()
print(number)
return number
def make_board(self):
board = Pedalboard([
Compressor(ratio=10, threshold_db=-20),
Gain(gain_db=self.dial.value()),
Phaser(),
Reverb()
],sample_rate=44100)
return board
def button_play(self):
print("")
def button_play2(self):
if self.qsound is not None:
board = self.make_board(self.effects)
self.effected_audio = board(self.audio,self.sr)
sd.play(self.effected_audio)
self.qsound2.play()
def button_stop(self):
if self.qsound is not None:
sd.stop()
self.qsound2.stop()
def button_openfile(self):
filepath, _ = QFileDialog.getOpenFileName(self, 'Open file','c:\\',"Audio files (*.wav)")
filepath = os.path.abspath(filepath)
self.qsound = QSound(filepath)
self.filename = os.path.basename(filepath)
self.audio, self.sr = librosa.load(self.filename, sr=44100)
self.label.setText(self.filename)
self.label.adjustSize()
def button_openfile2(self):
filepath2, _ = QFileDialog.getOpenFileName(self, 'Open file','c:\\',"Audio files (*.wav)")
filepath2 = os.path.abspath(filepath2)
self.qsound2 = QSound(filepath2)
self.filename2 = os.path.basename(filepath2)
self.label2.setText(self.filename2)
self.label2.adjustSize()
def start(self):
self.thread.start()
def execute(self):
self.update_progress(0)
self.thread = QThread()
self.worker = Worker(self)
self.worker.moveToThread(self.thread)
self.thread.started.connect(self.worker.run)
self.worker.finished.connect(self.thread.quit)
self.worker.finished.connect(self.worker.deleteLater)
self.thread.finished.connect(self.thread.deleteLater)
self.worker.progress.connect(self.update_progress)
self.thread.start()
self.button.setEnabled(False)
def update_progress(self, progress):
self.progress.setValue(progress)
self.button.setEnabled(progress == 100)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
app.exec_()

Qt Docs for moveToThread() say:
Changes the thread affinity for this
object and its children. The object
cannot be moved if it has a parent.
I cannot see where Worker instance gets started. Maybe you wanted to derive Worker from QThread, and call self.worker.start()?
OutputStream and the Qt GUI both have their own internal main loops which are responsible for handling their respective events. When you execute both on the same thread, one or the other might freeze.

Related

PyQt PySide 2 QObject::killTimer: Timers cannot be stopped from another thread

I know this might be repeating the same question that someone else asked already, but I still don't understand what is happening actually. I have seen a lot of examples for multithreading in this forum and have compiled a dummy demo of what my actual application going to do. Here is the code:
import sys
from PySide2.QtCore import *
from PySide2.QtWidgets import *
from PySide2.QtGui import *
class Color(QWidget):
def __init__(self, color):
super(Color, self).__init__()
self.setAutoFillBackground(True)
palette = self.palette()
palette.setColor(QPalette.Window, QColor(color))
self.setPalette(palette)
class Inlet_Thread(QThread):
data = Signal(int)
tabs = Signal(int)
finish = Signal()
def __init__(self):
super().__init__()
self._stopped = False
self.init_timers()
print('new thread')
self.c = 0
self.d = 0
def init_timers(self):
self.timer1 = QTimer(self)
self.timer1.timeout.connect(self.routine)
self.timer2 = QTimer(self)
self.timer2.timeout.connect(self.routine2)
def starter(self):
self.timer1.start(1000)
self.timer2.start(2000)
def run(self):
while not self._stopped:
pass
self.timer1.stop()
self.timer2.stop()
print('stopped')
self.finish.emit()
def routine(self):
self.data.emit(self.c)
self.c += 1
def routine2(self):
self.tabs.emit(self.d)
self.d += 1
#Slot()
def stop(self):
self._stopped = True
class Inlet_Worker(QObject):
data = Signal(int)
tabs = Signal(int)
def __init__(self):
super().__init__()
self._stopped = False
self._registered = False
self.init_timers()
self.c = 0
self.d = 0
def init_timers(self):
self.timer1 = QTimer(self)
self.timer1.timeout.connect(self.routine)
self.timer2 = QTimer(self)
self.timer2.timeout.connect(self.routine2)
def starter(self):
self.timer1.start(1000)
self.timer2.start(2000)
def routine(self):
self.data.emit(self.c)
self.c += 1
def routine2(self):
self.tabs.emit(self.d)
self.d += 1
#Slot()
def stop(self):
self.timer1.stop()
self.timer2.stop()
print('stopped')
class Window(QWidget):
startSig = Signal()
stopSig = Signal()
def __init__(self):
super().__init__()
self.dlg = Dialog()
self.thread = None
pagelayout = QVBoxLayout(self)
button_layout = QHBoxLayout()
self.stacklayout = QStackedLayout()
pagelayout.addLayout(button_layout)
pagelayout.addLayout(self.stacklayout)
self.button = QPushButton('Start')
self.button2 = QPushButton('Stop')
self.button3 = QPushButton('Window')
button_layout.addWidget(self.button)
button_layout.addWidget(self.button2)
button_layout.addWidget(self.button3)
self.stacklayout.addWidget(Color("red"))
self.stacklayout.addWidget(Color("yellow"))
self.stacklayout.addWidget(Color("green"))
self.button.clicked.connect(self.startThr2)
self.button2.clicked.connect(self.stopThr)
self.button3.clicked.connect(self.showDlg)
#Slot(int)
def switch_tab(self, d):
idx = d % 3
print(idx)
self.stacklayout.setCurrentIndex(idx)
def showDlg(self):
if not self.dlg.isVisible():
self.dlg.show()
def startThr(self):
self.thread = QThread()
self.worker = Inlet_Worker()
self.worker.moveToThread(self.thread)
self.worker.data.connect(self.dlg.update)
self.worker.tabs.connect(self.switch_tab)
self.stopSig.connect(self.worker.stop)
self.thread.started.connect(self.worker.starter)
self.thread.start()
def startThr2(self):
if self.thread is None or not self.thread.isRunning():
self.thread = Inlet_Thread()
self.thread.started.connect(self.thread.starter)
self.thread.data.connect(self.dlg.update)
self.thread.tabs.connect(self.switch_tab)
self.thread.finish.connect(self.finished)
self.stopSig.connect(self.thread.stop)
self.thread.start()
def stopThr(self):
self.stopSig.emit()
def finished(self):
print('thread finished')
self.thread.quit()
self.thread.wait()
def closeEvent(self, event):
#self.stopThr()
pass
class Dialog(QWidget):
def __init__(self):
super().__init__()
self.text1 = QLabel("Label 1 : ")
self.text2 = QLabel("Text2")
layout = QHBoxLayout(self)
layout.addWidget(self.text1)
layout.addWidget(self.text2)
self.setGeometry(400, 100, 100, 50)
#Slot(int)
def update(self, sig):
self.text2.setText(str(sig))
def closeEvent(self, event):
pass
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.setGeometry(750, 80, 300, 80)
window.show()
sys.exit(app.exec_())
While the threading is performing fine, it returns an error:
QObject::killTimer: Timers cannot be stopped from another thread
And if I use the Qobject and move it to a thread, an additional error appears
QObject::~QObject: Timers cannot be stopped from another thread
Can someone explain what is causing the error?
EDIT:
I changed the code to be more complex and show where each data will go. The reason I'm using Qtimer in a thread is that there are multiple signals to emit and the frequency and shape of each data is different. Since this code is only a simplification of my actual application, there might be some different parts. And if there are alternatives, I'm open to suggestions.

Windump via a python subprocess and PyQt Button

The simple idea is that user inputs duration in seconds, and presses a PyQt button, that calls a function that creates a python subprocess and runs windump via it. Then time sleep is used to wait for user defined duration and then process.terminate(), terminates it (code below)
def windump_exec(duration):
p = s.Popen(['windump', '-i', '3', '-w', 'packets.pcap'], stdout=s.PIPE)
time.sleep(duration)
p.terminate()
Now once this is done, scapy reads .pcap file and I show stuff on the screen in short. While this is happening QWaitingSpinner is running, and to handle this I run the above logic (including scapy) using QRunnable (code below)
class ThreadRunnable(QRunnable):
def __init__(self, _time, filler):
QRunnable.__init__(self)
self.time = _time
self.filler = filler
self.signal = RunnableSignal()
def run(self):
windump_exec(self.time)
packets = parse_data()
self.filler(packets)
self.signal.result.emit()
The Problem is that the windump code works fine on it's own, but inside the QThread it doesn't create an output file and hence scapy has nothing to read (open), and it gives error.
Instead of using Popen with QThread you can use QProcess, in my test I have used tcpdump but I suppose that changing to windump should have the same behavior:
import os
from PyQt5 import QtCore, QtGui, QtWidgets
from scapy.all import rdpcap
import psutil
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
class DumpProcesor(QtCore.QObject):
started = QtCore.pyqtSignal()
finished = QtCore.pyqtSignal()
def __init__(self, parent=None):
super().__init__(parent)
self._process = QtCore.QProcess()
self._timer = QtCore.QTimer(singleShot=True)
self._timer.timeout.connect(self.handle_timeout)
self._pid = -1
#property
def process(self):
return self._process
#property
def timer(self):
return self._timer
#QtCore.pyqtSlot()
def start(self):
self.started.emit()
status, self._pid = self._process.startDetached()
if status:
self._timer.start()
else:
self.finished.emit()
#QtCore.pyqtSlot()
def handle_timeout(self):
if self._pid > 0:
p = psutil.Process(self._pid)
p.terminate()
QtCore.QTimer.singleShot(100, self.finished.emit)
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.log_te = QtWidgets.QTextEdit(readOnly=True)
self.time_sb = QtWidgets.QSpinBox(minimum=1)
self.start_btn = QtWidgets.QPushButton(self.tr("Start"))
grid_layout = QtWidgets.QGridLayout(self)
grid_layout.addWidget(self.log_te, 0, 0, 1, 3)
grid_layout.addWidget(QtWidgets.QLabel("Time (seg):"), 1, 0)
grid_layout.addWidget(self.time_sb, 1, 1)
grid_layout.addWidget(self.start_btn, 1, 2)
self.dump_procesor = DumpProcesor(self)
self.dump_procesor.process.setProgram("tcpdump")
filename = os.path.join(CURRENT_DIR, "packets.pcap")
self.dump_procesor.process.setArguments(["-i", "3", "-w", filename])
self.start_btn.clicked.connect(self.start)
self.dump_procesor.finished.connect(self.on_finished)
#QtCore.pyqtSlot()
def start(self):
self.log_te.clear()
self.start_btn.setDisabled(True)
self.dump_procesor.timer.setInterval(self.time_sb.value() * 1000)
self.dump_procesor.start()
#QtCore.pyqtSlot()
def on_finished(self):
self.start_btn.setDisabled(False)
filename = os.path.join(CURRENT_DIR, "packets.pcap")
packets = rdpcap(filename)
for packet in packets:
t = packet.show(dump=True)
self.log_te.append(t)
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()

No changes of GUI during event? (Python3.6, PyQt5)

I'm trying to change style of button during action.
When button is pressed, I want to make them blue, then read rfid card and then by the ID change color of button to green or red (in code is just red to make it easier).
The problem is that changing style of button to blue do nothing (green and red works fine).
It is waiting to finish 'clicked()' method?
How to tell to PyQt5 "Do it now!" ?
Edit: I modified the pseudocode to reproducible Example.
#!/usr/bin/python3
import sys
import time
from PyQt5.QtWidgets import *
class MainScreen(QWidget):
def __init__(self):
QWidget.__init__(self)
self.button = QPushButton("TEXT")
self.button.clicked.connect(self.clicked)
self.changeButtonColor("black") # set default style
self.grid = QGridLayout(self)
self.grid.addWidget(self.button)
self.show()
def changeButtonColor(self, color):
self.button.setStyleSheet("QPushButton{ \
color: "+color+"; font: bold 18px;}")
def clicked(self):
self.changeButtonColor("blue") # nothing happening (this is my problem!)
uid = self.readCard() # reading ID from rfid card
self.changeButtonColor("red")
def readCard(self):
#return rfid.read() # in real case
time.sleep(2)
return "12345678"
def main():
app = QApplication(sys.argv)
window = MainScreen()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
void QTimer::singleShot(int msec, const QObject *receiver, const char *member)
This static function calls a slot after a given time interval.
It is very convenient to use this function because you do not need to bother with a timerEvent or create a local QTimer object.
import sys
import time
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
class MainScreen(QWidget):
def __init__(self):
QWidget.__init__(self)
self.button = QPushButton("TEXT")
self.button.clicked.connect(self.clicked)
self.changeButtonColor("black")
self.grid = QGridLayout(self)
self.grid.addWidget(self.button)
self.show()
def changeButtonColor(self, color):
self.button.setStyleSheet("QPushButton{ \
color: "+color+"; font: bold 18px;}")
def clicked(self):
self.changeButtonColor("blue") # nothing happening (this is my problem!)
# time.sleep(2) # reading ID from rfid card
# self.changeButtonColor("red")
QtCore.QTimer.singleShot(2000, lambda: self.changeButtonColor("red")) # <---
def main():
app = QApplication(sys.argv)
window = MainScreen()
sys.exit(app.exec_())
Update
import sys
import time
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import *
class Worker(QtCore.QObject): # +++
started = QtCore.pyqtSignal()
finished = QtCore.pyqtSignal()
data = QtCore.pyqtSignal(str)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.running = False
self.stop = 5
#QtCore.pyqtSlot()
def read_data_from_sensor(self):
self.started.emit()
time.sleep(1) # We simulate the blocking process
while self.running and self.stop:
dt = time.strftime("%Y-%m-%d %H:%M:%S")
self.data.emit(dt)
time.sleep(1) # We simulate the blocking process
self.stop -= 1
self.finished.emit()
class MainScreen(QWidget):
def __init__(self):
QWidget.__init__(self)
self.button = QPushButton("TEXT Start")
self.button.clicked.connect(self.clicked)
self.changeButtonColor("black")
self.label_data = QtWidgets.QLabel(self, alignment=QtCore.Qt.AlignCenter)
self.label_data.setText('Pending')
self.grid = QGridLayout(self)
self.grid.addWidget(self.label_data)
self.grid.addWidget(self.button)
self.show()
### vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
self._worker = Worker()
self._worker.started.connect(self.on_started)
self._worker.finished.connect(self.on_finished)
self._worker.data.connect(self.update_label)
self._thread = QtCore.QThread(self)
self._thread.start()
self._worker.moveToThread(self._thread)
#QtCore.pyqtSlot()
def on_started(self):
self.label_data.setText("Start to read")
self.button.setText("TEXT Stop")
self.button.setEnabled(True)
self._worker.stop = 5
#QtCore.pyqtSlot()
def on_finished(self):
self.label_data.setText("Pending")
self.button.setText("TEXT Start")
self.button.setEnabled(True)
self.changeButtonColor("red") # <---
self._worker.running = False
self._worker.stop = 5
#QtCore.pyqtSlot(str)
def update_label(self, data):
self.label_data.setText(data)
### ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
def changeButtonColor(self, color):
self.button.setStyleSheet("QPushButton{ \
color: "+color+"; font: bold 18px;}")
def clicked(self):
self.changeButtonColor("blue") # nothing happening (this is my problem!)
# time.sleep(2) # reading ID from rfid card
# self.changeButtonColor("red")
# QtCore.QTimer.singleShot(2000, lambda: self.changeButtonColor("red")) # <---
### vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
if self._worker.running:
self._worker.running = False
else:
self._worker.running = True
QtCore.QTimer.singleShot(0, self._worker.read_data_from_sensor)
self.button.setEnabled(False)
### ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
def main():
app = QApplication(sys.argv)
window = MainScreen()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
install quamash and qtawesome
#!/usr/bin/python3
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QSize
import asyncio
from quamash import QEventLoop
import qtawesome as qt
class MainScreen(QWidget):
oop = None
def __init__(self, _loop):
try:
QWidget.__init__(self)
self.loop = _loop
self.button = QPushButton("TEXT")
self.button.clicked.connect(self.synced_click)
self.button.setStyleSheet("color: black;")
self.grid = QGridLayout(self)
self.grid.addWidget(self.button)
self.show()
except Exception as e:
print(e)
def synced_click(self):
try:
asyncio.ensure_future(self.clicked(), loop=self.loop)
except Exception as e:
print('error')
print(e)
#asyncio.coroutine
async def clicked(self):
try:
spin_icon = qt.icon('fa5s.spinner', color='red', animation=qt.Spin(self.button))
self.loop.call_soon_threadsafe(self.button.setIconSize, QSize(12, 12))
self.loop.call_soon_threadsafe(self.button.setIcon, spin_icon)
self.loop.call_soon_threadsafe(self.button.setText, "progress")
self.loop.call_soon_threadsafe(self.button.setStyleSheet, "color: red;")
await asyncio.sleep(3)
tick_icon = qt.icon('fa5s.check-circle', color='blue')
self.loop.call_soon_threadsafe(self.button.setStyleSheet, "color:blue;")
self.loop.call_soon_threadsafe(self.button.setText, "done")
self.loop.call_soon_threadsafe(self.button.setIcon, tick_icon)
await asyncio.sleep(1)
except Exception as e:
print('error')
print(e)
if __name__ == "__main__":
import sys
loop = QEventLoop(QApplication(sys.argv))
asyncio.set_event_loop(loop=loop)
with loop:
window = MainScreen(loop)
loop.run_forever()

How can i run two programs parallelly using Thread with pyqt4

Here in my sample program i want to display my gif upto my process is completed.After my process is completed automatically my gif will close.for that i used here threading function but i am not able to show gif.But i don't have any idea where i did mistake in this program,So can any one please check this program and please guide me.Thank you in advance.
Given below is my code:
import sys
from PyQt4 import QtGui
import os
class DialogThread(QtCore.QThread):
def __init__(self, interval=1):
self.interval = interval
thread = threading.Thread(target=self.run, args=())
thread.daemon = True # Daemonize thread
thread.start() # Start the execution
def run(self):
global savingDataFlag,dialog_gif
""" Method that runs forever """
while True:
if savingDataFlag == True:
dialog_gif.run()
print 'in saving data true loop'
else:
dialog_gif.close()
print 'in saving data false loop'
time.sleep(3)
class Dialog(QtGui.QDialog):
def __init__(self, parent = None):
super(Dialog, self).__init__(parent)
self.setStyleSheet("QDialog {background-color:black; color:white }")
self.label1 = QtGui.QLabel(
text="Please Wait while It Is Processing The Data...",
font=QtGui.QFont("Times", 20,weight=QtGui.QFont.Bold)
)
self.label21 = QtGui.QLabel()
vbox1 = QtGui.QVBoxLayout(self)
vbox1.addWidget(self.label1)
vbox1.addWidget(self.label21)
self.resize(640, 480)
#QtCore.pyqtSlot()
def show_gif(self):
movie = QtGui.QMovie(
"./img/loading1.gif",
parent=self
)
self.label21.setMovie(movie)
movie.start()
self.exec_()class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
okButton = QtGui.QPushButton("Save")
cancelButton = QtGui.QPushButton("Cancel")
okButton.clicked.connect(self.save)
hbox = QtGui.QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(okButton)
hbox.addWidget(cancelButton)
vbox = QtGui.QVBoxLayout()
vbox.addStretch(1)
vbox.addLayout(hbox)
self.setLayout(vbox)
self.setGeometry(300, 300, 300, 150)
self.setWindowTitle('Buttons')
self.show()
def save(self):
self.d1 = QtGui.QWidget()
self.form1 = QtGui.QFormLayout(self.d1)
self.name_file = QtGui.QLabel(("File Name:"))
self.line_edit1 = QtGui.QLineEdit()
self.saveBtn1 = QtGui.QPushButton(("Save"))
self.saveBtn1.clicked.connect(self.saveModelWidget)
self.canclebtn = QtGui.QPushButton(("Cancel"))
self.form1.addRow(self.name_file, self.line_edit1)
self.form1.addRow( self.canclebtn,self.saveBtn1)
self.d1.setWindowTitle("Enter Grids")
self.d1.setGeometry(450,300,500,100)
self.d1.show()
def saveModelWidget(self):
self.savefile()
def savefile(self):
self.d1.close()
self.save_text = self.line_edit1.text()
self.currFilePath = os.getcwd()
if self.save_text.endsWith(".csv"):
file_Name= self.save_text
with open(file_Name, 'w+')as f:
dir_path1 =os.getcwd()
save_filename = os.path.join(dir_path1,file_Name)
print "prrocess start"
# here some execution
# when i click the save button i want to display the gif file till to complete the process in save file
print "process ends"
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You are using QThread with the threading module, and the QThread does not use it since it never starts it, instead you are using threading to execute the run () method. On the other hand overwriting the run method of QThread is a method but many limits many things, instead I will create a worker that will live in another thread. Another error is that movie is a local variable that will be deleted so it will not show, the solution is to pass it a parent. Considering the above the solution is:
import os
import sys
import threading
from PyQt4 import QtCore, QtGui
class SaveWorker(QtCore.QObject):
started = QtCore.pyqtSignal()
finished = QtCore.pyqtSignal()
#QtCore.pyqtSlot(str)
def save_file(self, filename):
self.started.emit()
for i in range(100000):
print(i)
with open(filename, 'w+')as f:
print(filename)
# some execution
self.finished.emit()
class GifDialog(QtGui.QDialog):
def __init__(self, parent = None):
super(GifDialog, self).__init__(parent)
self.setStyleSheet("QDialog {background-color:black; color:white }")
self.label1 = QtGui.QLabel(
text="Please Wait while It Is Processing The Data...",
font=QtGui.QFont("Times", 20,weight=QtGui.QFont.Bold)
)
self.label2 = QtGui.QLabel()
vbox = QtGui.QVBoxLayout(self)
vbox.addWidget(self.label1)
vbox.addWidget(self.label2)
#QtCore.pyqtSlot()
def show_gif(self):
movie = QtGui.QMovie(
"./img/loading1.gif",
parent=self
)
self.label2.setMovie(movie)
movie.start()
self.show()
class FormDialog(QtGui.QDialog):
def __init__(self, parent=None):
super(FormDialog, self).__init__(parent)
self.setWindowTitle("Enter Grids")
self.line_edit = QtGui.QLineEdit()
self.savebtn = QtGui.QPushButton(
text="Save",
clicked=self.save
)
self.cancelbtn = QtGui.QPushButton("Cancel")
#self.savebtn.clicked.connect(self.saveModelWidget)
flay = QtGui.QFormLayout(self)
flay.addRow("File Name:", self.line_edit)
flay.addRow(self.cancelbtn, self.savebtn)
self.worker = SaveWorker()
self.gif_dialog = GifDialog(self)
self.worker.started.connect(self.gif_dialog.show_gif)
self.worker.finished.connect(self.close)
#QtCore.pyqtSlot()
def save(self):
filename = str(self.line_edit.text())
if filename.endswith(".csv"):
save_filename = os.path.join(os.getcwd(), filename)
thread = threading.Thread(target=self.worker.save_file, args=(save_filename,))
thread.daemon = True
thread.start()
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
okButton = QtGui.QPushButton(
text="Save",
clicked=self.on_clicked
)
cancelButton = QtGui.QPushButton("Cancel")
# okButton.clicked.connect(self.save)
hbox = QtGui.QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(okButton)
hbox.addWidget(cancelButton)
vbox = QtGui.QVBoxLayout(self)
vbox.addStretch(1)
vbox.addLayout(hbox)
#QtCore.pyqtSlot()
def on_clicked(self):
dialog = FormDialog()
dialog.exec_()
def main():
app = QtGui.QApplication(sys.argv)
dialog = Example()
dialog.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

How to update the progress bar using with pyqt4

Here it is sample program i want to update the my progress bar using with pyqt4.and i want to show th3 30% data saving and another 60% data processing.I am executing the my program it is aborting.Can any one please help me how to update the my progress bar.Thank you in advance.
Given below is my code:
import sys
import time
from pyface.qt import QtGui, QtCore
global X,Y
X= 5
Y= 4
import threading
class SaveWorker(QtCore.QObject):
progress_update = QtCore.Signal(int)
def save_file(self):
while True:
MyCustomWidget().updateProgressBar()
class Dialog(QtGui.QDialog):
def __init__(self, parent = None):
super(Dialog, self).__init__(parent)
self.setStyleSheet("QDialog {background-color:black; color:white }")
self.label1 = QtGui.QLabel(
text="Please Wait...",
font=QtGui.QFont("Times", 20,weight=QtGui.QFont.Bold)
)
self.progress = QtGui.QProgressBar()
self.box = QtGui.QVBoxLayout()
self.label2 = QtGui.QLabel()
vbox = QtGui.QVBoxLayout(self)
vbox.addWidget(self.label1)
vbox.addLayout(self.box)
self.show_gif()
def show_gif(self):
self.progress = QtGui.QProgressBar()
self.progress.setRange(0,100)
self.box.addWidget(self.progress)
self.show()
class MyCustomWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(MyCustomWidget, self).__init__(parent)
self.worker = SaveWorker()
self.gif_dialog = Dialog()
self.worker.progress_update.connect(self.gif_dialog.show_gif)
thread = threading.Thread(target=self.worker.save_file)
thread.daemon = True
thread.start()
self.progressPer = 0
fileList = []
processes = []
_dataSavingPer = 30.0/(X*Y)
for i in range(X*Y):
name = 'file'+str(i+1) + ".txt"
fileList.append(name)
self.progressPer += _dataSavingPer
self.updateProgressBar(self.progressPer)
#updating the progress bar
_dataProcessPer = 60.0/(X*Y)
for file in fileList:
process = 'fileProcess'+str(i+1) + ".txt"
processes.append(process)
self.progressPer += _dataProcessPer
self.updateProgressBar(self.progressPer)
#Updating the progressPer
#how can i update these two values in to progressbar
def updateProgressBar(self,value):
self.gif_dialog.progress.setValue(value)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MyCustomWidget()
sys.exit(app.exec_())
I do not understand what you tried when writing the following:
class SaveWorker(QtCore.QObject):
progress_update = QtCore.Signal(int)
def save_file(self):
while True:
MyCustomWidget().updateProgressBar()
updateProgressBar requires a value what value are you going through?, on the other hand when using MyCustomWidget() you are creating an object different from the one shown, and no MyCustomWidget object should be created in another thread.
What you have to do is move the heavy task to the save_file method since it will be executed in another thread:
import sys
import threading
from pyface.qt import QtGui, QtCore
X, Y = 5, 4
class SaveWorker(QtCore.QObject):
progressChanged = QtCore.Signal(int)
def save_file(self):
fileList = []
processes = []
_dataSavingPer = 30.0/(X*Y)
progress = 0
for i in range(X*Y):
name = 'file'+str(i+1) + ".txt"
fileList.append(name)
progress += _dataSavingPer
self.progressChanged.emit(progress)
_dataProcessPer = 60.0/(X*Y)
for file in fileList:
process = 'fileProcess'+str(i+1) + ".txt"
processes.append(process)
progress += _dataProcessPer
self.progressChanged.emit(progress)
class Dialog(QtGui.QDialog):
def __init__(self, parent = None):
super(Dialog, self).__init__(parent)
self.setStyleSheet("QDialog {background-color:black; color:white }")
self.label1 = QtGui.QLabel(
text="Please Wait...",
font=QtGui.QFont("Times", 20,weight=QtGui.QFont.Bold)
)
self.progress = QtGui.QProgressBar()
self.box = QtGui.QVBoxLayout()
self.label2 = QtGui.QLabel()
vbox = QtGui.QVBoxLayout(self)
vbox.addWidget(self.label1)
vbox.addLayout(self.box)
self.show_gif()
def show_gif(self):
self.progress = QtGui.QProgressBar()
self.progress.setRange(0,100)
self.box.addWidget(self.progress)
self.show()
class MyCustomWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(MyCustomWidget, self).__init__(parent)
self.worker = SaveWorker()
self.gif_dialog = Dialog()
self.worker.progressChanged.connect(self.gif_dialog.progress.setValue)
thread = threading.Thread(target=self.worker.save_file)
thread.daemon = True
thread.start()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MyCustomWidget()
sys.exit(app.exec_())

Categories

Resources