taste-progress-dialog.py 1.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#!/usr/bin/env python

import sys
import PySide
import time
import signal

from PySide.QtCore import QThread, Signal, QObject

from PySide.QtGui import (QApplication,
                          QMessageBox,
                          QProgressDialog)

class MyThread(QThread, QObject):
    signal = Signal(str)
    progress = Signal(int)
    quit = Signal()
    def run(self):
        value = 10
        while True:
            # read from stdin without any buffering
            line = sys.stdin.readline()
            if len(line) == 0:
                print ("Bye")
                self.quit.emit()
                return
            elif line[0] == 'q':
                print ("Quit")
                self.quit.emit()
                return
            else:
Maxime Perrotin's avatar
Maxime Perrotin committed
32 33 34 35 36 37 38 39 40
                try:
                    split = line.split()
                    possible_val = split[0]
                    value = int(possible_val)
                    text = ' '.join(split[1:])
                    self.progress.emit(value)
                except (ValueError, IndexError):
                    text = line
                self.signal.emit(text)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
                time.sleep(0.1)



def run_gui():
        app = QApplication(sys.argv)
        thread = MyThread()       
        progress = QProgressDialog()
        progress.setValue(0)
        thread.signal.connect(progress.setLabelText)
        thread.quit.connect(progress.cancel)
        thread.progress.connect(progress.setValue)
        thread.start()
        progress.exec_()

def main():
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    run_gui()

if __name__ == '__main__':
    main()