I'm attempting to make a gui to run and distribute a toolbox of scripts for my job and I'm struggling to get the QT console to interact properly. I want to have the embbeded console with a row of buttons above it that can trigger the different scripts. My button currently will run a file inside the terminal of my editor so I know that the button is working.
Here is my current gui which is setup pretty much how I want it visually:basic gui with one button above a jypuiter embed qtconsole
and here is my current code:
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_())
I'm prenty stumped this is my first time trying to make a gui, I've only ever done more robotic or machine-learning algorithm based things with code outside of classes.
I've tried to find other resources and have found these but either they are outdated or I'm missing some thing of it.