2014-10-29 96 views
0

我已經寫了一個python代碼,其中每個按鈕按下我正在使用2個線程的幫助執行我的工作。 例如。在當前正在執行的線程死亡之前等待

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 

只要按下按鈕,直到目前它簡化版,打印屏幕上的O/P,按鈕顯示「WIP」(工作正在進行中)

我想進一步修改, 當任何按鈕被按下我不希望用戶在一個按鈕正在進行時(WIP)按下更多按鈕。 所以其他線程不應該在當前工作的兩個線程終止之前創建。 我該怎麼做?

我給我在這裏的代碼,以供參考:

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_() 

回答

相關問題