gpt4 book ai didi

python - PyQT 段错误(有时)

转载 作者:行者123 更新时间:2023-11-28 21:21:18 24 4
gpt4 key购买 nike

我正在创建一个界面来管理一些服务器,像重启服务这样的基本功能,我是这样想的,我在QTCreator中创建了界面,在PyQT5中编写了程序,它工作正常(有时)。

有时它也能正常工作,其他时候我得到 Segmentation FaultQObject::connect: Cannot queue arguments of type 'QTextCursor'
(确保使用 qRegisterMetaType() 注册了“QTextCursor”。)

当我在 MainWindow 中点击 restart_nginx 时发生,可能是线程管理的一些问题,这是我使用 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_())

enter image description here

最佳答案

切勿尝试从工作线程内更改 GUI。只从主 GUI 线程中更新 GUI,不要将对 GUI 元素的引用传递给工作线程。

在 GUI 线程和工作线程之间进行通信的最简单和最安全的方法是发出 custom signals .当信号为 emitted across threads以这种方式,它们被排队,一旦控制返回到它的事件循环,槽将在接收者的线程中被调用。

在 PyQt 中,通常也建议使用 pyqtSlot decorator在跨线程建立连接时,原因在 this answer 中列出.

关于python - PyQT 段错误(有时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21857935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com