Python 为什么PyQt4插槽多次接收一个信号
我使用pyuic4创建GUI并创建一个线程,该线程完成后,它会将结果附加到textbrowser。 问题是: 当我在“主机或URL”中输入一些文本时,点击“DoCheck”按钮,它会打印一次“Thisistext”。但当我从URL更改文本时,再按一下那个按钮,它会打印两次,以后每次都会增加。日志说信号只发出一次,但插槽函数被多次调用。我不知道我错过了什么。有人能提出解决办法吗? 谢谢你能提供的任何帮助 这是我的主要任务Python 为什么PyQt4插槽多次接收一个信号,python,multithreading,pyqt4,Python,Multithreading,Pyqt4,我使用pyuic4创建GUI并创建一个线程,该线程完成后,它会将结果附加到textbrowser。 问题是: 当我在“主机或URL”中输入一些文本时,点击“DoCheck”按钮,它会打印一次“Thisistext”。但当我从URL更改文本时,再按一下那个按钮,它会打印两次,以后每次都会增加。日志说信号只发出一次,但插槽函数被多次调用。我不知道我错过了什么。有人能提出解决办法吗? 谢谢你能提供的任何帮助 这是我的主要任务 import logging from PyQt4 import QtGui
import logging
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import QMessageBox
from form1 import *
logging.basicConfig(filename="netchecker.log", filemode='w',\
format='%(asctime)s %(levelname)s %(message)s',
level=logging.DEBUG)
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class PingThread(QtCore.QThread):
def run(self):
logging.debug("*****Done pingThread")
all_res = "this is text"
self.emit(QtCore.SIGNAL("Data"), all_res)
class MyApp(Ui_MainWindow):
def __init__(self, app):
logging.info("Init MyAPP")
self.app = app
self.pingThread = PingThread()
def setupUi(self, *args):
super(MyApp, self).setupUi(*args)
logging.info("setupUi")
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.doCheck)
def append_to_text_browser(self, newtext):
"""This method is callback for some thread run separated
from main thread"""
logging.debug("Inside append_to_text_browser")
self.textBrowser.append(newtext)
self.app.processEvents()
def onNewUrl(self):
font = QtGui.QFont()
self.pushButton.setFont(font)
del font
self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Do new check", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setEnabled(True)
self.app.processEvents()
def doCheck(self):
logging.info("doCheck beginning")
self.textBrowser.clear()
QtCore.QObject.connect(self.pingThread, \
QtCore.SIGNAL("Data"), \
self.append_to_text_browser)
logging.info("Called pingThread")
self.pingThread.start()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
MainWindow.setWindowIcon(QtGui.QIcon('zChecker.png'))
ui = MyApp(app)
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
以及由pyuic4 form1.py转换的文件:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created: Thu Jul 12 17:21:45 2012
# by: PyQt4 UI code generator 4.9.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.setEnabled(True)
MainWindow.resize(679, 713)
font = QtGui.QFont()
font.setKerning(True)
MainWindow.setFont(font)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.lineEdit = QtGui.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(100, 10, 561, 27))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 10, 81, 31))
self.label.setObjectName(_fromUtf8("label"))
self.checkBox_1 = QtGui.QCheckBox(self.centralwidget)
self.checkBox_1.setGeometry(QtCore.QRect(70, 50, 121, 22))
self.checkBox_1.setChecked(True)
self.checkBox_1.setObjectName(_fromUtf8("checkBox_1"))
self.checkBox_2 = QtGui.QCheckBox(self.centralwidget)
self.checkBox_2.setGeometry(QtCore.QRect(70, 80, 161, 22))
self.checkBox_2.setChecked(True)
self.checkBox_2.setObjectName(_fromUtf8("checkBox_2"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setEnabled(True)
self.pushButton.setGeometry(QtCore.QRect(510, 60, 98, 27))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
font.setKerning(True)
self.pushButton.setFont(font)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.textBrowser = QtGui.QTextBrowser(self.centralwidget)
self.textBrowser.setGeometry(QtCore.QRect(20, 110, 641, 521))
self.textBrowser.viewport().setProperty("cursor", QtGui.QCursor(QtCore.Qt.ArrowCursor))
self.textBrowser.setObjectName(_fromUtf8("textBrowser"))
self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
self.pushButton_2.setEnabled(False)
self.pushButton_2.setGeometry(QtCore.QRect(310, 630, 111, 31))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.checkbox_vital = QtGui.QCheckBox(self.centralwidget)
self.checkbox_vital.setEnabled(True)
self.checkbox_vital.setGeometry(QtCore.QRect(260, 50, 181, 22))
self.checkbox_vital.setToolTip(_fromUtf8(""))
self.checkbox_vital.setStatusTip(_fromUtf8(""))
self.checkbox_vital.setObjectName(_fromUtf8("checkbox_vital"))
self.checkbox_full = QtGui.QCheckBox(self.centralwidget)
self.checkbox_full.setEnabled(False)
self.checkbox_full.setGeometry(QtCore.QRect(260, 80, 141, 22))
self.checkbox_full.setToolTip(_fromUtf8(""))
self.checkbox_full.setObjectName(_fromUtf8("checkbox_full"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 679, 27))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.lineEdit, QtCore.SIGNAL(_fromUtf8("textEdited(QString)")), self.pushButton.showMenu)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "Network and website\'s health Checker - by MinhCD", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("MainWindow", "Host or URL", None, QtGui.QApplication.UnicodeUTF8))
self.checkBox_1.setText(QtGui.QApplication.translate("MainWindow", "Ping all parts", None, QtGui.QApplication.UnicodeUTF8))
self.checkBox_2.setText(QtGui.QApplication.translate("MainWindow", "TraceRoute all parts", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Do check", None, QtGui.QApplication.UnicodeUTF8))
self.textBrowser.setHtml(QtGui.QApplication.translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'Ubuntu\'; font-size:11pt; font-weight:400; font-style:normal;\">\n"
"<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p>\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-style:italic;\">Check results will be displayed here:</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_2.setText(QtGui.QApplication.translate("MainWindow", "Send report", None, QtGui.QApplication.UnicodeUTF8))
self.checkbox_vital.setText(QtGui.QApplication.translate("MainWindow", "Load vital components", None, QtGui.QApplication.UnicodeUTF8))
self.checkbox_full.setText(QtGui.QApplication.translate("MainWindow", "Load full site", None, QtGui.QApplication.UnicodeUTF8))
#-*-编码:utf-8-*-
#从读取ui文件“test.ui”生成的表单实现
#
#创建时间:2012年7月12日星期四17:21:45
#作者:PyQt4用户界面代码生成器4.9.1
#
#警告!在此文件中所做的所有更改都将丢失!
从PyQt4导入QtCore、QtGui
尝试:
_fromUtf8=QtCore.QString.fromUtf8
除属性错误外:
_fromUtf8=λs:s
类Ui_主窗口(对象):
def设置UI(自我,主窗口):
MainWindow.setObjectName(_fromUtf8(“MainWindow”))
MainWindow.setEnabled(真)
主窗口。调整大小(679713)
font=QtGui.QFont()
字体设置字距(真)
MainWindow.setFont(字体)
self.centralwidget=QtGui.QWidget(主窗口)
self.centralwidget.setObjectName(\u fromUtf8(“centralwidget”))
self.lineEdit=QtGui.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(100,10561,27))
self.lineEdit.setObjectName(_fromUtf8(“lineEdit”))
self.label=QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10,10,81,31))
self.label.setObjectName(_fromUtf8(“label”))
self.checkBox_1=QtGui.QCheckBox(self.centralwidget)
self.checkBox_1.setGeometry(QtCore.QRect(70,50,121,22))
self.checkBox_1.setChecked(True)
self.checkBox\u 1.setObjectName(\u fromUtf8(“checkBox\u 1”))
self.checkBox_2=QtGui.QCheckBox(self.centralwidget)
self.checkBox_2.setGeometry(QtCore.QRect(70,80,161,22))
self.checkBox_2.setChecked(True)
self.checkBox_2.setObjectName(_fromUtf8(“checkBox_2”))
self.pushButton=QtGui.QPushButton(self.centralwidget)
self.butdown.setEnabled(真)
自身按钮设置几何(QtCore.QRect(510,60,98,27))
font=QtGui.QFont()
font.setBold(真)
字体设置重量(75)
字体设置字距(真)
self.butdown.setFont(字体)
self.butdown.setObjectName(_fromUtf8(“butdown”))
self.textBrowser=QtGui.QTextBrowser(self.centralwidget)
self.textBrowser.setGeometry(QtCore.QRect(201110641521))
self.textBrowser.viewport().setProperty(“游标”,QtGui.QCursor(QtCore.Qt.ArrowCursor))
self.textBrowser.setObjectName(_fromUtf8(“textBrowser”))
self.pushButton_2=QtGui.QPushButton(self.centralwidget)
自动按钮_2.设置启用(错误)
自身按钮2.设置几何(QtCore.QRect(310、630、111、31))
self.butdown_2.setObjectName(_fromUtf8(“butdown_2”))
self.checkbox_vital=QtGui.QCheckBox(self.centralwidget)
self.checkbox\u vital.setEnabled(True)
self.checkbox_vital.setGeometry(QtCore.QRect(260,50181,22))
self.checkbox\u vital.setToolTip(\u fromUtf8(“”))
self.checkbox\u vital.setStatistip(\u fromUtf8(“”))
self.checkbox\u vital.setObjectName(\u fromUtf8(“checkbox\u vital”))
self.checkbox_full=QtGui.QCheckBox(self.centralwidget)
self.checkbox\u full.setEnabled(False)
self.checkbox_full.setGeometry(QtCore.QRect(260,80,141,22))
self.checkbox\u full.setToolTip(\u fromUtf8(“”))
self.checkbox\u full.setObjectName(\u fromUtf8(“checkbox\u full”))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar=QtGui.QMenuBar(主窗口)
self.menubar.setGeometry(QtCore.QRect(0,0679,27))
self.menubar.setObjectName(_fromUtf8(“menubar”))
MainWindow.setMenuBar(self.menubar)
self.statusbar=QtGui.QStatusBar(主窗口)
self.statusbar.setObjectName(_fromUtf8(“statusbar”))
main window.setStatusBar(self.statusbar)
自重传(主窗口)
QtCore.QObject.connect(self.lineEdit,QtCore.SIGNAL(_fromUtf8(“textexedited(QString)”)),self.button.showMenu)
QtCore.QMetaObject.connectSlotsByName(主窗口)
def重新传输(自身,主窗口):
MainWindow.setWindowTitle(QtGui.QApplication.translate(“MainWindow”,“网络和网站的健康检查器-由MinhCD提供”,无,QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate(“主窗口”、“主机或URL”、无、QtGui.QApplication.UnicodeUTF8))
self.checkBox_1.setText(QtGui.QApplication.translate(“主窗口”,“Ping所有部分”,无,QtGui.QApplication.UnicodeUTF8))
self.checkBox_2.setText(QtGui.QApplication.translate(“主窗口”,“TraceRoute所有部分”,无,QtGui.QApplication.UnicodeUTF8))
self.butdown.setText(QtGui.QApplication.translate(“主窗口”,“执行检查”,无,QtGui.QApplication.UnicodeUTF8))
self.textBrowser.setHtml(QtGui.QApplication.translate(“主窗口”),“\n”
“\n”
p,li{空白:预换行;}\n
“\n”
“
\n”
“检查结果将显示在此处:
”,无,QtGui.QApplication.unicodef8))
self.butdown_2.setText(QtGui.QApplication.translate(“主窗口”,“发送报告”,无,QtGui.QApplication.UnicodeUTF8))
self.checkbox_vital.setText(QtGui.QApplication.translate)(“主窗口”,“加载重要组件”,无,QtGui.QAppl
QtCore.QObject.connect(self.pingThread, \
QtCore.SIGNAL("Data"), \
self.append_to_text_browser)
def setupUi(self, *args):