Import threading
Import sys, time
From pyqt4 import qtcore, qtgui
Import psutil
Import OS
Class mainwindow (qtgui. qmainwindow ):
Def _ init _ (Self ):
Super (mainwindow, self). _ init __()
Self. main_layout = qtgui. qvboxlayout ()
OK _button = qtgui. qpushbutton ("Run ")
OK _button.clicked.connect (self. OK)
Self. main_layout.addwidget (OK _button)
Cancel_button = qtgui. qpushbutton ("cancel ")
Cancel_button.clicked.connect (self. Cancel)
Self. main_layout.addwidget (cancel_button)
Central_widget = qtgui. qwidget ()
Central_widget.setlayout (self. main_layout)
Self. setcentralwidget (central_widget)
Def myevenlistener (self, stop_event ):
State = true
While State and not stop_event.isset ():
For I in range (10,100 ):
Time. Sleep (I * 1, 0.01)
Print '.' * I
Def OK (Self ):
Self. stop_event = threading. Event ()
Self. c_thread = threading. Thread (target = self. myevenlistener, argS = (self. stop_event ,))
Self. c_thread.start ()
Def cancel (Self ):
Self. stop_event.set ()
Self. Close ()
Def main ():
APP = qtgui. qapplication (SYS. argv)
Window = mainwindow ()
Window. Resize (480,320)
Window. Show ()
App.exe C _()
Main ()
Def kill_proc_tree (PID, including_parent = true ):
Parent = psutil. Process (PID)
If including_parent:
Parent. Kill ()
Me = OS. getpid ()
Kill_proc_tree (me)
:
Pyqtthreading (provided by netizens)