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

时间:2018-12-16 17:02:57

标签: python pyqt pyqt5 qtconsole

我有一个相当大的项目,带有嵌入式Jupyter QtConsole 4.4.2。

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

现在,当某些进程正在运行/完成时,我尝试将一些文本发送到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_())  

任何想法都会受到高度赞赏!

祝一切顺利, 特里丰

1 个答案:

答案 0 :(得分:1)

您遇到的问题是,每次键入library(dplyr) library(highcharter) library(xts) library(tidyverse) locationAnalysis <- cc %>% group_by(Location.Description) %>% summarise(Total = n()) %>% arrange(desc(Total)) crimetypeAnalysis <- cc %>% group_by(Primary.Type) %>% summarise(Total = n()) %>% arrange(desc(Total)) hchart(crimetypeAnalysis, "column", hcaes(x = Primary.Type, y = Total, color = Total)) %>% hc_title(text = "Crime Types") %>% hc_subtitle(text = "(2001 - 2016)") %>% hc_xAxis(title = list(text = "Type")) %>% hc_yAxis(title = list(text = "Crimes")) %>% hc_colorAxis(stops = color_stops(n = 10, colors = c("#d98880", "#85c1e9", "#82e0aa"))) %>% hc_add_theme(hc_theme_darkunica()) %>% hc_legend(enabled = FALSE) 时您都在创建hc_colorAxis(stops = color_stops(n = 10, colors = c("#d98880", "#85c1e9", "#82e0aa"))),而是创建该类的对象并将其设置为该类的属性:

ConsoleWidget_embed