无法启动新的python线程

2024-10-04 09:25:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下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_())

Tags: textimportselfdefsyscontexttranslateqtgui
2条回答

很可能,线程中发生了异常。要识别并修复它,请尝试以下操作:

def logging_thread(self):
    try:
        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"))
    except:
        import traceback
        traceback.print_exc

确定错误原因后,有两个选项:

  1. 如果错误是您所做的(例如,属性输入错误),请完全删除try...except块。你知道吗
  2. 如果代码中存在合法发生的条件,则保留块,但使异常类型特定。例如except ValueError:。另外,在这种情况下,还要改进实际的处理程序。你知道吗

调用方法,而不是将其作为参数传递给新的Thread,如下所示:

t = threading.Thread(target=self.logging_thread()).start()

更改为:

t = threading.Thread(target=self.logging_thread).start()

相关问题 更多 >