MyQThread new

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@version: 001
@author: jianpan
@file: MyQThread.py
@time: 2017/6/3 11:54
"""
from PyQt4.QtCore import *
import Tkinter as tk
import multiprocessing
import os
import psutil


class MyQThread(QThread):
    def __init__(self, parent=None):
        super(MyQThread, self).__init__(parent)
        self.working = True
        self.num = 0

    def __del__(self):
        self.working = False
        self.wait()
        self.pid = 0

    def run(self):
        pass

    def open(self):
        try:
            os.popen('taskkill /pid %s  -f' % (str(self.pid)))
        except Exception, e:
            print e
        pool = multiprocessing.Pool(processes=1)
        print os.getpid()
        for i in xrange(1):
            pool.apply_async(func,)  # 维持执行的进程总数为processes,当一个进程执行完毕后会添加新的进程进去
        t = psutil.Process()
        print "Process :", t.pid
        pool.close()
        children = t.children()
        for child in children:
            print('Child pid is {}'.format(child.pid))
            self.pid = child.pid
        # pool.join()  # 调用join之前,先调用close函数,否则会出错。执行完close后不会有新的进程加入到pool,join函数等待所有子进程结束
        print "Sub-process(es) done."


def func():
    root = WarningTK()


class MyQThreadSleep60(QThread):
    def __init__(self, parent=None):
        super(MyQThreadSleep60, self).__init__(parent)
        self.working = True
        self.num = 0

    def __del__(self):
        self.working = False
        self.wait()

    def run(self):
        while self.working == True:
            file_str = 'File index {0}'.format(self.num)
            self.num += 1
            self.sleep(10)
            self.emit(SIGNAL('output(QString)'), file_str)



class MyQThread_30(QThread):
    def __init__(self, parent=None):
        super(MyQThread_30, self).__init__(parent)
        self.working = True
        self.num = 0

    def __del__(self):
        self.working = False
        self.wait()

    def run(self):
        while self.working == True:
            file_str = 'File index {0}'.format(self.num)
            self.num += 1
            self.sleep(20)
            self.emit(SIGNAL('output(QString)'), file_str)


class MyQThreadDelay(QThread):
    def __init__(self, parent=None):
        super(MyQThreadDelay, self).__init__(parent)
        self.working = True
        self.num = 0

    def run(self):
        os.popen('warning.mp3')
        print 'finish'


class MyQThread_Warning(QThread):
    def __init__(self, parent=None):
        super(MyQThread_Warning, self).__init__(parent)
        self.working = True
        self.num = 0

    def run(self):
        print '1'

    def open(self):
        self.root = WarningTK()
        self.root.mainloop()

    def getDlg(self):
        return self.root.destroy

    def close(self):
        try:
            self.root.quit()
            self.root.destroy()
        except Exception, e:
            print e


class WarningTK(tk.Tk):
    def __init__(self, master=None):
        tk.Tk.__init__(self, master)
        self.title('test')
        self.withdraw()
        screenwidth = self.winfo_screenwidth()
        screenheight = self.winfo_screenheight() - 100
        self.resizable(False, False)
        # 添加组件
        self.title("Warning!!")
        frame = tk.Frame(self, relief=tk.RIDGE, borderwidth=3)
        frame.pack(fill=tk.BOTH, expand=1)  # pack() 放置组件若没有则组件不会显示
        # 窗口显示的文字、并设置字体、字号
        label = tk.Label(frame, text="You have been working 30 minutes! Please have a break!!", \
                         font="Monotype\ Corsiva -20 bold")
        label.pack(fill=tk.BOTH, expand=1)
        # 按钮的设置
        self.button1 = tk.Button(frame, text="OK", font="Cooper -25 bold", fg="red", command=self.destroy)
        self.button1.pack(side=tk.BOTTOM)

        self.update_idletasks()
        self.deiconify()  # now the window size was calculated
        self.withdraw()  # hide the window again 防止窗口出现被拖动的感觉 具体原理未知?
        self.geometry(
            '%sx%s+%s+%s' % (self.winfo_width() + 10, self.winfo_height() + 10,
                             (screenwidth - self.winfo_width()) / 2,
                             (screenheight - self.winfo_height()) / 2))
        self.deiconify()
        self.mainloop()


posted @ 2017-06-04 17:45  我的大金金  阅读(246)  评论(0编辑  收藏  举报