如何使qtconsole到的QThread下工作吗?(How to make qtconsole t

2019-10-28 14:57发布

问题的最小工作示例如下。 它工作正常,但如果我上运行jupyter外壳有些沉重的任务,它挂起GUI,直到任务完成。

您可能会遇到这个问题的滚动条,如果你执行类似这将挂起:

In [1]: n = 50000000
   ...: while n > 0:
   ...:     n -=1
   ...:     x = n**4

不幸的是这也发生在主GUI框架(其上qtconsole嵌入到)。

现在的问题是,如何设置一个单独的QThread,在我的主要pyqt5 GUI的后台执行qtconsole?

我看到“Spyder的” IDE有我想要的东西,(即嵌入式IPython的外壳,它运行的作业在后台工具),但我不能从他们的项目了解他们是如何到达那里。

import sys
from PyQt5 import QtWidgets, QtCore 

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.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()

        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()


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

    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 = ConsoleWidget_embed()
    main.show()
    sys.exit(app.exec_())  
文章来源: How to make qtconsole to work under Qthread?