我正在创建一个界面来管理一些服务器,基本功能如重启服务和这样思考,我在QTCreator
中创建了界面并在PyQT5
中编程,它正常工作(有时候)。
有时它也可以正常工作,有时候我会Segmentation Fault
或QObject::connect: Cannot queue arguments of type 'QTextCursor'
(Make sure 'QTextCursor' is registered using qRegisterMetaType().)
当我在restart_nginx
中点击MainWindow
时,可能会出现线程管理方面的一些问题,这是我与PyQT/QT
合作的第一个项目
谢谢。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import paramiko
import threading
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from ui_mainwindow import Ui_MainWindow
servers = {
"Mars": "198.58.112.***",
"Saturn": "198.58.116.***"
}
green = "rgb(83, 236, 17)"
default = "rgb(154, 154, 154)"
yellow = "yellow"
red = "red"
pw = 'pass'
def format_status(color):
return "background-color: " + color + "; border: 1px solid black;"
class Worker(QObject):
def __init__(self, ui, server, command=None, sudo=False, *args, **kwargs):
QObject.__init__(self, *args, **kwargs)
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.ui = ui
self.command = command
self.sudo = sudo
self.server = server
finished = pyqtSignal()
@pyqtSlot()
def execute(self):
self.ui.status_restart_nginx.setStyleSheet(format_status(color=yellow))
try:
self.ssh.connect(self.server, username='hercules', password=pw)
except paramiko.SSHException as e:
self.ui.log.append("ERRO: Impossible to connect: %s", e)
self.ui.status_restart_nginx.setStyleSheet(format_status(color=red))
try:
session = self.ssh.get_transport().open_session()
session.get_pty()
f = session.makefile()
session.exec_command("ls -lh")
#if self.sudo:
# session.send(pw + '\n')
for line in f.readlines():
self.ui.log.append(line)
f.close()
self.ssh.close()
self.ui.status_restart_nginx.setStyleSheet(format_status(color=green))
except Exception as e:
self.ui.status_restart_nginx.setStyleSheet(format_status(color=red))
print 'errinho: ', e
self.finished.emit()
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None, **kwargs):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.btn_restart_nginx.clicked.connect(self.restart_nginx)
self.thread_worker = None
self.obj_worker = None
def create_worker(self, server, command=None, sudo=False):
self.thread_worker = QThread(parent = self)
self.obj_worker = Worker(ui = self, server = server, command = command, sudo = sudo)
self.obj_worker.moveToThread(self.thread_worker)
self.obj_worker.finished.connect(self.thread_worker.quit)
self.thread_worker.started.connect(self.obj_worker.execute)
self.thread_worker.start()
def restart_nginx(self):
self.status_restart_nginx.setStyleSheet(format_status(color=yellow))
self.log.append("(Mars) Nginx: trying to restart front-end...")
self.create_worker(servers["Mars"], command = "sudo service nginx restart", sudo = True)
if __name__=="__main__":
from sys import argv, exit
app = QApplication(argv)
m = MainWindow()
m.show()
m.raise_()
exit(app.exec_())
答案 0 :(得分:6)
永远不要尝试从工作线程中更改GUI。只能从主GUI线程中更新GUI,并且不要将对GUI元素的引用传递给工作线程。
GUI线程和工作线程之间通信的最简单,最安全的方法是发出custom signals。当信号以这种方式emitted across threads时,它们就会排队,一旦控制返回到它的事件循环,就会在接收者的线程中调用该槽。
在PyQt中,出于pyqtSlot decorator中列出的原因,通常建议在跨线程进行连接时使用this answer。