foo():
while True:
#do smthng
boo():
while True:
#do smthng
I will use this two method whenever my button pushed as:
Thread(target=foo).start()
Thread(target=boo).start()
In my code foo is button and for filling progress bar. boo is button and for undo filling progress bar.
When I press foo trigger button there is no problem shows when I press boo trigger button if get empty then I press foo again I got segmentation error.
How can I stop foo thread when boo pressed and boo thread when foo pressed?
Edit: My all code is:
import time
import sys
from PyQt4 import QtGui, QtCore
import threading
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_X11InitThreads)
class CustomThread(threading.Thread):
def __init__(self, target=None):
super(CustomThread, self).__init__(target=target)
self._stop = threading.Event()
def stop(self):
self._stop.set()
class CustomProgressBar(QtGui.QProgressBar):
def __init__(self, x, y, lenght, height, require_list=[]):
QtGui.QProgressBar.__init__(self)
self.require_list = require_list
self.setGeometry(x, y, lenght, height)
self.speed = 0
def fill(self):
while self.speed > 0 and self.value() < 100:
self.setValue(self.value()+self.speed)
time.sleep(0.01)
while self.speed < 0 and self.value() > 0:
self.setValue(self.value()+self.speed)
time.sleep(0.01)
self.speed = 0
class Valve(QtGui.QCheckBox):
def __init__(self, x, y, lenght, height, inputs=[], outputs=[]):
super(Valve, self).__init__()
self.sources = inputs
self.outputs = outputs
self.setGeometry(x, y, lenght, height)
self.stateChanged.connect(self.filler)
self.fill_thread_list = []
def is_fillable(self):
for source in self.sources:
if source.value() == 100:
return 1
return 0
def filler(self):
for thread in self.fill_thread_list:
thread.stop()
for output in self.outputs:
if self.is_fillable():
fillThread = CustomThread(target=output.fill)
self.fill_thread_list.append(fillThread)
if self.isChecked():
output.speed = 1
else:
output.speed = -1
fillThread.start()
class MainWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.initUI()
def initUI(self):
layout = QtGui.QHBoxLayout()
#pipes and buttons
self.pipe = CustomProgressBar(20, 50, 100, 2)
self.pipe.setValue(100)
self.pipe2 = CustomProgressBar(120, 50, 100, 2, require_list=[self.pipe])
self.pipe3 = CustomProgressBar(120, 50, 100, 2, require_list=[self.pipe])
self.button1 = Valve(100, 50, 10, 10, inputs=[self.pipe], outputs=[self.pipe2, self.pipe3])
#add buttons to layout
layout.addWidget(self.button1)
layout.addWidget(self.pipe)
layout.addWidget(self.pipe2)
layout.addWidget(self.pipe3)
self.setLayout(layout)
self.setGeometry(0, 0, 1366, 768)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
It just different from this question.