There's a lot of stuff in your code that is unrelated to your problem. Try to make a short as possible program that reproduces your problem and write it according to pep8. If you do these things you are likely to get useful responses.