我正在使用Python创建一个控制台驱动的Qt应用程序。我想嵌入IPython Qt控制台,而不是实现我自己的自定义控制台,但也让它响应我的应用程序。例如,我希望某些关键字输入到控制台以触发我的主应用程序中的操作。所以我在控制台中输入“dothis”,在我的应用程序的另一个窗口中显示一个图表。
我在这些方面看到了一些问题:this one讨论了如何将IPython Qt小部件嵌入到应用程序中并传递函数,尽管看起来这些函数在IPython内核中执行而不是在内核中执行我的主应用程序。还有this guy,但是我不能在示例中执行代码(它已经两年了),看起来它看起来不像我想做的那样。
有没有办法可以传入将在我的主内核中执行的函数或方法,或者至少通过与IPython内核通信来模拟这种行为?有人曾经这样做过吗?
答案 0 :(得分:3)
这就是我想出来的,到目前为止它运作良好。我继承RichIPythonWidget类并重载_execute
方法。每当用户在控制台中键入内容时,我会根据已注册的命令列表进行检查;如果它匹配一个命令,那么我执行命令代码,否则我只是将输入传递给默认的_execute
方法。
控制台代码:
from IPython.qt.console.rich_ipython_widget import RichIPythonWidget
class CommandConsole( RichIPythonWidget ):
"""
This is a thin wrapper around IPython's RichIPythonWidget. It's
main purpose is to register console commands and intercept
them when typed into the console.
"""
def __init__(self, *args, **kw ):
kw['kind'] = 'cc'
super(CommandConsole, self).__init__(*args, **kw)
self.commands = {}
def _execute(self, source, hidden):
"""
Overloaded version of the _execute first checks the console
input against registered commands. If it finds a command it
executes it, otherwise it passes the input to the back kernel
for processing.
"""
try:
possible_cmd = source.split()[0].strip()
except:
return super(CommandConsole, self)._execute("pass", hidden)
if possible_cmd in self.commands.keys():
# Commands return code that is passed to the console for execution.
s = self.commands[possible_cmd].execute()
return super(CommandConsole, self)._execute( s, hidden )
else:
# Default back to the original _execute
return super(CommandConsole, self)._execute(source, hidden)
def register_command( self, name, command ):
"""
This method links the name of a command (name) to the function
that should be called when it is typed into the console (command).
"""
self.commands[name] = command
示例命令:
from PyQt5.QtCore import pyqtSignal, QObject, QFile
class SelectCommand( QObject ):
"""
The Select command class.
"""
# This signal is emitted whenever the command is executed.
executed = pyqtSignal( str, dict, name = "selectExecuted" )
# This is the command as typed into the console.
name = "select"
def execute(self):
"""
This method is executed whenever the ``name`` command is issued
in the console.
"""
name = "data description"
data = { "data dict" : 0 }
# The signal is sent to my Qt Models
self.executed.emit( name, data )
# This code is executed in the console.
return 'print("the select command has been executed")'