pyqt5 应用程序中的嵌入式 qtconsole 无法按预期工作

embedded qtconsole in pyqt5 aplication does not works as expected

我有一个相当大的项目,其中嵌入了 Jupyter QtConsole 4.4.2。

有关详细信息,请参阅: https://github.com/3fon3fonov/trifon

现在,虽然有些进程是 running/finished,但我正在尝试向 Qtconsole 发送一些文本并将其用作输出屏幕(反之亦然,我希望 Jupyter 能够接收如果需要,可以控制 GUI,但这是我稍后必须处理的另一个问题。

问题是 "ConsoleWidget" 函数的某些固有功能 似乎不起作用,我找不到原因...

例如在我的主要 GUI 代码中:

ConsoleWidget_embed().push_vars({'fit':fit}) <-- WORKS!   

ConsoleWidget_embed().clear() <-- Does not work!
ConsoleWidget_embed().print_text("Test string")  <-- Does not work!

这是我嵌入到 Qtab 中的代码。

import numpy as np
import sys #,os
from PyQt5 import QtCore, QtGui, QtWidgets, uic

from qtconsole.rich_jupyter_widget import RichJupyterWidget
from qtconsole.inprocess import QtInProcessKernelManager
from qtconsole.console_widget import ConsoleWidget


class ConsoleWidget_embed(RichJupyterWidget,ConsoleWidget):
    global fit

    def __init__(self, customBanner=None, *args, **kwargs):
        super(ConsoleWidget_embed, self).__init__(*args, **kwargs)

        if customBanner is not None:
            self.banner = customBanner

        #self.font_size = 4
        self.kernel_manager =   QtInProcessKernelManager()
        self.kernel_manager.start_kernel(show_banner=True)
        self.kernel_manager.kernel.gui = 'qt'
        self.kernel = self.kernel_manager.kernel
        self.kernel_client = self._kernel_manager.client()
        self.kernel_client.start_channels()

        #self._execute("kernel = %s"%fit, False) 

        def stop():
            self.kernel_client.stop_channels()
            self.kernel_manager.shutdown_kernel()
            self.guisupport.get_app_qt().exit()

        self.exit_requested.connect(stop)


    def push_vars(self, variableDict):
        """
        Given a dictionary containing name / value pairs, push those variables
        to the Jupyter console widget
        """
        self.kernel_manager.kernel.shell.push(variableDict)

    def clear(self):
        """
        Clears the terminal
        """
        self._control.clear()

        # self.kernel_manager

    def print_text(self, text):
        """
        Prints some plain text to the console
        """
        self._append_plain_text(text, before_prompt=False)

    def execute_command(self, command):
        """
        Execute a command in the frame of the console widget
        """
        self._execute(command, False)




if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    main = mainWindow()
    main.show()
    sys.exit(app.exec_())  

任何想法将不胜感激!

一切顺利, 特里丰

您遇到的问题是每次键入 ConsoleWidget_embed() 时都会创建一个 ConsoleWidget_embed,而不是创建一个 class 的对象并将其设置为class:

    self.console_widget = ConsoleWidget_embed()
    self.terminal_embeded.addTab(self.console_widget, "Jupyter")

    # ...

def jupiter_push_vars(self):
    global fit        
    self.console_widget.push_vars({'fit':fit})   
    self.console_widget.clear()
    self.console_widget.print_text("Test string")