from
PyQt4.QtCore
import
*
from
PyQt4.QtGui
import
*
from
PyQt4
import
QtGui, QtCore
import
sys, subprocess, time
class
myThread(QtCore.QThread):
def
__init__(
self
):
QtCore.QThread.__init__(
self
)
def
run(
self
):
self
.pipe
=
subprocess.Popen(
'ping 127.0.0.1'
, stdout
=
subprocess.PIPE)
while
self
.pipe.poll()
=
=
None
:
self
.emit(QtCore.SIGNAL(
"getLog(QString)"
), QtCore.QString.fromUtf8((
self
.pipe.stdout.readline().rstrip())))
time.sleep(
0.2
)
restInfo
=
self
.pipe.stdout.readlines()
if
len
(restInfo):
for
line
in
restInfo:
self
.emit(QtCore.SIGNAL(
"writeLog(QString)"
), QtCore.QString.fromUtf8((line.rstrip())))
class
MainWindow(
object
):
def
__init__(
self
, Dialog):
self
.bt
=
QtGui.QWidget(Dialog)
Dialog.setMinimumWidth(
400
)
Dialog.setMinimumHeight(
300
)
self
.button
=
QtGui.QPushButton(
self
.bt)
self
.button.setText(
'Start'
)
self
.button_2
=
QtGui.QPushButton(
self
.bt)
self
.button_2.setText(
'Stop'
)
self
.text
=
QtGui.QPlainTextEdit(
self
.bt)
self
.text.setFont(QtGui.QFont(
'Courier New'
,
10
))
self
.verticalLayout
=
QtGui.QVBoxLayout(Dialog)
self
.verticalLayout.addWidget(
self
.button)
self
.verticalLayout.addWidget(
self
.button_2)
self
.verticalLayout.addWidget(
self
.text)
self
.th1
=
myThread()
QtCore.QObject.connect(
self
.th1, QtCore.SIGNAL(
"getLog(QString)"
),
self
.writeLog)
QtCore.QObject.connect(
self
.button, QtCore.SIGNAL(
'clicked()'
),
self
.run)
QtCore.QObject.connect(
self
.button_2, QtCore.SIGNAL(
'clicked()'
),
self
.stop)
def
run(
self
):
self
.th1.start()
def
stop(
self
):
self
.th1.pipe.terminate()
def
writeLog(
self
,
str
):
self
.text.appendPlainText(
str
)
app
=
QtGui.QApplication(sys.argv)
dlg
=
QtGui.QDialog()
main
=
MainWindow(dlg)
dlg.show()
sys.exit(app.exec_())
联系客服