MyQThread

# coding=utf-8
from PyQt4.QtCore import *
import Tkinter as tk
import os
import time

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

    def run(self):
        while self.working == True:
            file_str = 'File index {0}'.format(self.num)
            self.num += 1
            self.sleep(10)
            # print 'jianpan'
            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):
        # filename = r'music_delay.mp3'
        # mp3 = mp3play.load(filename)
        # mp3.play()
        # time.sleep(mp3.seconds())
        # print mp3.isplaying()
        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()


原文地址:https://www.cnblogs.com/jian-pan/p/6934847.html