0

I have written a python code where with each button press I'm executing my work with the help of 2 threads. eg.

 Press button 1 -> Thread-1 and Thread-2 will do the job
 Press button 2 -> Thread-3 and Thread-4 will do the job
 Press button 3 -> Thread-5 and Thread-6 will do the job

Whenever button is pressed, till the moment it does't print the o/p on screen, button displays 'WIP'(Work In Progress)

I want to modify it further, When any of the button is pressed I don't want the user to press more buttons when one button is in progress(WIP). So other threads shouldn't be created before the currently working two are terminated. How should I do this?

I'm giving here my code for reference:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys, os, time
import Queue
import threading

class LearnButton(QPushButton):
    def __init__(self, title, test):
        super(QPushButton, self).__init__()
        self._title = title
        self._test = test
        self.setText(title)
        self.clicked.connect(self.click_threads)
        self._q = Queue.Queue()
        self.flag = True

    def click_threads(self):
        self.thread1 = threading.Thread(target=self.fun1)
        self.thread2 = threading.Thread(target=self.fun2)
        self.thread1.start()
        self.thread2.start()

    def fun1(self):
        print "Val", self._test, self._title, type(self._test), type(self._title)
        self.date_time_string = time.strftime("%Y%m%d_%H%M%S")
        self.retainedv = self.date_time_string
        print self.date_time_string
        self.flag = False


    def fun2(self):
        print "Val", self._test, self._title, type(self._test), type(self._title)
        self.setEnabled(False)
        while self.thread1.isAlive():
            self.setText('WIP')
            time.sleep(0.5)
        self.setEnabled(True)
        self.setText(self._title)


class LearnApp(QDialog):
    def __init__(self):
        super(QDialog, self).__init__()
        self.setWindowTitle("LearnApp")

        close_button = QPushButton("Close")
        close_button.clicked.connect(self.close)
        self.button1 = LearnButton("B1", "Im in B1")
        self.button2 = LearnButton("B2", "Im in B2")
        self.button3 = LearnButton("B3", "Im in B3")

        self.test_report = QTextEdit()
        self.test_report.setReadOnly(True)
        layout = QHBoxLayout()        
        sub_layout = QVBoxLayout()
        sub_layout.addWidget(self.button1)
        sub_layout.addWidget(self.button2)
        sub_layout.addWidget(self.button3)
        sub_layout.addWidget(close_button)
        layout.addLayout(sub_layout)
        layout.addWidget(self.test_report)
        self.setLayout(layout)
        self.setFocus()


app = QApplication(sys.argv)
dialog = LearnApp()
dialog.show()
app.exec_()
tryPy
  • 71
  • 1
  • 11

1 Answers1

0

You need your threads to emit signals that you will use to suspend your GUI. Since you will be using Qt signal/slot mechanism, you shouldn't use Python threading module. PyQt has its own threading modules (QThreading for ex).

See: Threading in a PyQt application: Use Qt threads or Python threads?

Community
  • 1
  • 1
Plouff
  • 3,290
  • 2
  • 27
  • 45