gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:34:04 26 4
gpt4 key购买 nike

我有一个相当大的项目,其中嵌入了 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_())

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

祝一切顺利,特里丰

最佳答案

您的情况的问题是,您每次键入 ConsoleWidget_embed() 时都会创建一个 ConsoleWidget_embed,而是创建该类的一个对象并将其设置为属性类(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")

关于python - pyqt5 应用程序中嵌入的 qtconsole 无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53804511/

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