我有一个带有嵌入式 Jupyter QtConsole 的 Qt 应用程序。这是我用来创建 Jupyter 控制台的代码:
from qtconsole.rich_jupyter_widget import RichJupyterWidget
from qtconsole.inprocess import QtInProcessKernelManager
class EmbeddedJupyterConsole(RichJupyterWidget):
def __init__(self, gui):
super(RichJupyterWidget, self).__init__(gui.ipython_container)
self.kernel_manager = QtInProcessKernelManager()
self.kernel_manager.start_kernel()
self.kernel = self.kernel_manager.kernel
self.kernel.gui = 'qt4'
self.kernel_client = self.kernel_manager.client()
self.kernel_client.start_channels()
gui.ipython.kernel_manager = self.kernel_manager
gui.ipython.kernel_client = self.kernel_client
gui.ipython.exit_requested.connect(self.stop)
def executeCmd(self, cmd):
self.kernel.shell.ev(cmd)
def pushVar(self, **kwarg):
self.kernel.shell.push(kwarg)
def stop(self):
self.kernel_client.stop_channels()
self.kernel_manager.shutdown_kernel()
self.app.exit()
这是我用来生成主 Qt 窗口的代码:
class QtMainWindow(QMainWindow):
def __init__(self,app):
self.console = EmbeddedJupyterConsole(self)
# Initialize other widgets within the main window
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = QtMainWindow(app)
sys.exit(app.exec_())
每当我运行此代码时,Jupyter 控制台和 QtApplication 都会正常工作。但是,我也看到以下错误:
Traceback (most recent call last):
File "C:\...\Miniconda2\lib\site-packages\qtconsole\console_widget.py", line 464, in sizeHint
font_metrics = QtGui.QFontMetrics(self.font)
AttributeError: 'EmbeddedJupyterConsole' object has no attribute 'font'
如果我向 EmbeddedJupyterConsole 添加一个“假”字体属性,那么我会失去控制台的良好格式并且错误仍然出现。有谁知道问题是什么以及如何解决?