PyQt错误:' PyQt4.QtCore.pyqtSignal'对象没有属性' connect'

时间:2015-12-26 10:25:06

标签: python pyqt

这是我的端口扫描程序代码。我想让它成为多线程,所以它需要使用信号。但是我得到了一个错误。

from __future__ import print_function
import socket
import threading
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

from ui import Ui_MainWindow


def clickable(widget):
    class Filter(QObject):

        clicked = pyqtSignal()

        def eventFilter(self, obj, event):

            if obj == widget:
                if event.type() == QEvent.MouseButtonRelease:
                    if obj.rect().contains(event.pos()):
                        self.clicked.emit()
                        return True

            return False

    filter = Filter(widget)
    widget.installEventFilter(filter)
    return filter.clicked


class Testhost(QObject):
    thost = pyqtSignal(str)

    def __init__(self, t_host, lip):
        QObject.__init__(self)
        self.t_host = t_host
        self.lip = lip

    def start_test(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(5)
        try:
            sock.connect((self.t_host, 80))
            # sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            # sock1.settimeout(1)
            # sock1.connect((self.t_host, 23))

            if self.lip == self.t_host:
                self.thost.emit("" + self.t_host + " L")
            else:
                self.thost.emit(self.t_host)

        except:
            pass


class Application(QMainWindow, Ui_MainWindow):
    thread_c = 0

    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        Testhost.thost.connect(self.appender)
        clickable(self.Start1).connect(lambda w=self.Ip.text(): self.scan(w))

    def scan(self, iprange):

        Testhost('192.168.1.1', '192')

    def appender(self, text):
        self.listWidget.addItem(text)


app = QApplication(sys.argv)
mainwindow = Application()
mainwindow.show()
app.exec_()

我收到错误:

Traceback (most recent call last):
  File "D:/Project/Python/Ice Net/Ice Net.py", line 105, in <module>
    mainwindow = Application()
  File "D:/Project/Python/Ice Net/Ice Net.py", line 63, in __init__
    Testhost.thost.connect(self.appender)
AttributeError: 'PyQt4.QtCore.pyqtSignal' object has no attribute 'connect'

我已尝试过所有解决方案,但它没有用。

1 个答案:

答案 0 :(得分:2)

你做得不正确,你需要先将信号绑定到t = Testhost('192.168.1.1', '192') t.thost.connect(self.appender) 类的实例然后执行信号连接,如下所示:

displayable()