如何通过Qt按钮在嵌入式QTConsole中运行脚本?
嵌入式Jupyter QTConsole交互问题:按钮触发脚本无法在控制台内运行
我正尝试为工作制作一个用于运行和分发脚本工具箱的GUI,但在实现QTConsole的正常交互方面遇到了困难。我希望在嵌入式控制台上方设置一排按钮,用以触发不同的脚本。目前我的按钮可以在编辑器的终端中运行文件,说明按钮功能正常,但无法在嵌入式QTConsole内执行并显示结果。
当前GUI视觉布局基本符合预期:嵌入式Jupyter QTConsole上方带有一个按钮的基础GUI
当前代码
import sys import logging from PySide6.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QVBoxLayout, QHBoxLayout, QWidget, QScrollBar from PySide6.QtCore import Qt, QCoreApplication #QT Console from qtconsole.rich_jupyter_widget import RichJupyterWidget from qtconsole.manager import QtKernelManager from qtconsole.inprocess import QtInProcessKernelManager from IPython.lib import guisupport # The ID of an installed kernel, e.g. 'bash' or 'ir'. USE_KERNEL = 'python3' def make_jupyter_widget_with_kernel(): global ipython_widget # Prevent from being garbage collected # Create an in-process kernel kernel_manager = QtInProcessKernelManager() kernel_manager.start_kernel(show_banner=False) kernel = kernel_manager.kernel kernel.gui = 'qt4' kernel_client = kernel_manager.client() kernel_client.start_channels() ipython_widget = RichJupyterWidget() ipython_widget.kernel_manager = kernel_manager ipython_widget.kernel_client = kernel_client return ipython_widget class ToolBoxWindow(QMainWindow): def __init__(self): super().__init__() # Main Widget and Layout main_widget = QWidget() self.setCentralWidget(main_widget) main_layout = QVBoxLayout(main_widget) self.setWindowTitle("SiPy Tools") # Function to add widget with label def add_widget_with_label(layout, widget, label_text): hbox = QHBoxLayout() label = QLabel(label_text) hbox.addWidget(label) hbox.addWidget(widget) layout.addLayout(hbox) # Function to add widget without label def add_widget(layout,widget): hbox = QHBoxLayout() hbox.addWidget(widget) layout.addLayout(hbox) # Function to combine 2 widgets (such as text box w/ scroll bar) def add_combo_widget(layout, widget1,widget2,space): hbox = QHBoxLayout() hbox.addWidget(widget1) hbox.addWidget(widget2) layout.setSpacing(0) layout.addLayout(hbox) # QPushButton self.button = QPushButton('Run Test') self.button.clicked.connect(self.on_button_clicked) add_widget_with_label(main_layout, self.button, 'QPushButton:') # QT jupyter console initialization self.jupyter_widget = make_jupyter_widget_with_kernel() add_widget_with_label(main_layout,self.jupyter_widget,"QTconsole") def shutdown_kernel(self): print("Shutting down kernel...") self.jupyter_widget.kernel_client.stop_channels() self.jupyter_widget.kernel_manager.shutdown_kernel() def on_button_clicked(self): #open and run the file inside of the QT console when clicked exec(open("test.py").read()) # Run the application app = QApplication(sys.argv) window = ToolBoxWindow() window.show() app.aboutToQuit.connect(window.shutdown_kernel) sys.exit(app.exec_())
已查找资源
我已查找过相关资源,但要么内容过时,要么未能找到解决要点:
- Embedding IPython Qt console in a PyQt application
- What's the right way to get an iPython/Jupyter embedded Qt console?
这是我首次尝试开发GUI,此前仅编写过机器人或机器学习算法相关的非类结构代码,目前十分困惑。
内容的提问来源于stack exchange,提问作者Caffeinatedinsanity
相关产品推荐
相关产品推荐

