使用QThread点击按钮2两次后,Pyqt GUI突然没有响应

时间:2017-06-14 03:59:49

标签: python pyqt pyqt4

我尝试为我的流程创建QThread课程,当我点击生成按钮时,我的QTextEdit中没有显示任何内容,当我再次点击按钮我的GUI突然 没有响应

我的代码

更新代码

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import ipaddress

class Stream(QObject):
    newText = pyqtSignal(str)

    def write(self, text):
        self.newText.emit(str(text))

    def flush(self):
        pass

class MyClass(object):
    def __init__(self, device_type=None, ip=None, username=None, password=None, secret=None, command=None):
        self.device_type = device_type
        self.ip = ip
        self.username = username
        self.password = password
        self.secret = secret
        self.command = command

class sshConnection(QThread):
    def __init__(self):
        QThread.__init__(self)

    def __del__(self):
        self.wait()

    def runSSH(self):
        #doo process
        print('Haii')
        for x in range(100):
            print(x)


class Widget(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent=parent)
        self.device_list = []  # object array
        self.setWindowTitle("Network Automation")
        self.setFixedSize(350,500)
        ############################# Input IP

        sys.stdout = Stream(newText=self.onUpdateText)

        # Device Type
        lb_device_list = QLabel(self)
        lb_device_list.setText('Device Type')
        self.cb_device_list = QComboBox(self)
        self.cb_device_list.addItem('cisco_ios')
        self.cb_device_list.addItem('cisco_s300')

        # Ip Device
        lb_ip = QLabel(self)
        lb_ip.setText('IP Address')
        self.le_ip = QLineEdit(self)
        self.le_ip.setText('')
        self.le_ip.setPlaceholderText('Input Device IP')
        self.le_ip.setFixedWidth(150)

        # username
        lb_username = QLabel(self)
        self.le_username = QLineEdit(self)
        lb_username.setText('Username')
        self.le_username.setText('')
        self.le_username.setPlaceholderText('Input Username')
        self.le_username.setFixedWidth(150)

        # password
        lb_password = QLabel(self)
        self.le_password = QLineEdit(self)
        lb_password.setText('Password')
        self.le_password.setText('')
        self.le_password.setPlaceholderText('Input Password')
        self.le_password.setFixedWidth(150)

        # Privilage Password
        lb_enable = QLabel(self)
        lb_enable.setText('Privilege Mode Password')
        self.le_enable = QLineEdit(self)
        self.le_enable.setText('')
        self.le_enable.setPlaceholderText('Input Enable Password')
        self.le_enable.setFixedWidth(150)

        # button generate and add
        btgenerate = QPushButton(self)
        btgenerate.setText('Generate')
        btgenerate.setFixedWidth(70)
        btadd = QPushButton(self)
        btadd.setText('Add')

        # button delete
        btdel = QPushButton(self)
        btdel.setFixedWidth(70)
        btdel.setText('Remove')

        # line
        line = QFrame(self)
        line.setFrameShape(QFrame.VLine)
        line.setFrameShadow(QFrame.Sunken)
        line.setLineWidth(3)

        #line 2
        line2 = QFrame(self)
        line2.setFrameShape(QFrame.HLine)
        line2.setFrameShadow(QFrame.Sunken)
        line2.setLineWidth(3)

        ########################### Layout Ip Device List

        lb3 = QLabel(self)
        lb3.setText('IP Device List')
        self.ip_device_list = QListWidget(self)
        self.ip_device_list.setFixedWidth(150)
        # self.combobox_ip_list = QComboBox(self)
        # self.combobox_ip_list.setFixedWidth(170)

        ############################## SubLayout and Layout
        hblayout = QHBoxLayout()
        hblayout.addWidget(btgenerate)
        hblayout.addWidget(btadd)

        ###############################  Processs
        processlabel = QLabel("Process",self)

        self.process = QTextEdit(self)
        self.process.setLineWrapColumnOrWidth(400)
        self.process.setLineWrapMode(QTextEdit.FixedPixelWidth)
        self.process.setReadOnly(True)


        sublayout = QVBoxLayout()
        sublayout.addWidget(lb_device_list)
        sublayout.addWidget(self.cb_device_list)
        sublayout.addWidget(lb_ip)
        sublayout.addWidget(self.le_ip)
        sublayout.addWidget(lb_username)
        sublayout.addWidget(self.le_username)
        sublayout.addWidget(lb_password)
        sublayout.addWidget(self.le_password)
        sublayout.addWidget(lb_enable)
        sublayout.addWidget(self.le_enable)
        sublayout.addLayout(hblayout)

        sublayout2 = QVBoxLayout()
        sublayout2.addWidget(lb3)
        sublayout2.addWidget(self.ip_device_list)
        #sublayout2.addWidget(self.combobox_ip_list)
        sublayout2.addWidget(btdel)
        sublayout2.addStretch(1)

        sublayout3 = QVBoxLayout()
        sublayout3.addWidget(processlabel)
        sublayout3.addWidget(self.process)

        layout = QGridLayout(self)
        layout.addLayout(sublayout, 0, 0)
        layout.addWidget(line, 0, 1)
        layout.addWidget(line2, 1, 0, 1, 3)
        #layout.addWidget(processlabel,2,0)
        layout.addLayout(sublayout3,2,0,2,3)
        layout.addLayout(sublayout2, 0, 2)

        btadd.clicked.connect(self.addDevice)
        btdel.clicked.connect(self.remove)
        btgenerate.clicked.connect(self.runThread)

    def onUpdateText(self, text):
        cursor = self.process.textCursor()
        cursor.movePosition(QTextCursor.End)
        cursor.insertText(text)
        self.process.setTextCursor(cursor)
        self.process.ensureCursorVisible()

    # ----------- AddDevice Process

    def addDevice(self):
        try:
            ip = self.le_ip.text()
            ipaddress.ip_address(ip)
            device_type = str(self.cb_device_list.currentText())
            username = self.le_username.text()
            password = self.le_password.text()
            secret = self.le_enable.text()
            command = 'show tech'
            coomand2 = ''
            self.device_list.append(MyClass(device_type, ip, username, password, secret, command))

            # self.combobox_ip_list.addItem(ip)# Add Ip to ComboBox
            self.ip_device_list.addItem(ip)

            self.le_ip.clear()
            self.le_username.clear()
            self.le_password.clear()
            self.le_enable.clear()
            for list in self.device_list:
                print(list.ip, list.device_type)
        except ValueError:
            print("insert you're ip correctly")
            QMessageBox.question(self, 'Warning', "Insert You're IP Corecctly")

    def remove(self):
        index = self.ip_device_list.currentRow()
        if index != -1:
            self.ip_device_list.takeItem(index)
            del self.device_list[index]

    def runThread(self):
        self.run_thread = sshConnection()
        self.run_thread.finished.connect(app.exit)
        self.run_thread.start()

app = QApplication(sys.argv)
app.setStyle('cleanlooks')
window = Widget()
window.show()
sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

QThread希望您实现run方法,我认为它与runSSH相同但不是,您可以做的是在run方法中调用它,我在下面显示的测试我删除了行self.run_thread.finished.connect(app.exit),因为它会在打印作业没有花费很长时间后立即关闭。

class sshConnection(QThread):
    def __init__(self):
        QThread.__init__(self)

    def __del__(self):
        self.wait()

    def run(self):
        self.runSSH()

    def runSSH(self):
        #doo process
        print('Haii')
        for x in range(100):
            print(x)