我有以下python代码。我想开始新的线程后,按钮被按下,因为我想做一些其他与主线程。但代码目前,我似乎没有创建一个新的线程时,从pycharm并发图检查。我只有按下按钮才能开始新的线程。此外,按下按钮后程序也没有响应。请帮忙。你知道吗
from PyQt4 import QtCore, QtGui
import sys
import subprocess
import re
import threading
sys.settrace
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class GuiMainWindow(QtGui.QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(420, 280)
self.centralwidget = QtGui.QWidget(MainWindow)
self.buttonTrans = QtGui.QPushButton(self.centralwidget)
self.buttonTrans.setGeometry(QtCore.QRect(50, 110, 131, 51))
self.buttonTrans.setObjectName(_fromUtf8("buttonTrans"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(70, 60, 281, 21))
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
print(self)
def retranslateUi(self, MainWindow):
self.buttonTrans.setText(_translate("MainWindow", "Start", None))
self.connect(self.buttonTrans, QtCore.SIGNAL('clicked()'), self.setup_video)
def setup_video(self):
print("Setting up VIDEO")
t = threading.Thread(target=self.logging_thread()).start()
def logging_thread(self):
cmd33 = "ping www.google.com"
cmd3process = subprocess.Popen(cmd33.split(), stdout=subprocess.PIPE, shell=True)
import time
while True:
output3 = cmd3process.stdout.readline()
time.sleep(1)
if output3 == '' and cmd3process.poll() != None:
break
print(output3.decode("utf-8"))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = GuiMainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
很可能,线程中发生了异常。要识别并修复它,请尝试以下操作:
确定错误原因后,有两个选项:
try...except
块。你知道吗except ValueError:
。另外,在这种情况下,还要改进实际的处理程序。你知道吗调用方法,而不是将其作为参数传递给新的
Thread
,如下所示:更改为:
相关问题 更多 >
编程相关推荐