Message printing to a console widget not working properly
wjassim opened this issue · 2 comments
Hello,
When I attempt to publish a message to a console widget with style='output,' an error occurs:
File ~\site-packages\pyqtgraph\console\repl_widget.py:139 in write
if style == 'output' and row == self._lastCommandRow + 1:
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'`
However, when we set style='command,' the code functions perfectly. Any suggestions?
Below is a minimal working example:
import sys
from PySide6 import QtWidgets
import pyqtgraph as pg
from pyqtgraph.dockarea import DockArea, Dock
from pyqtgraph.console import ConsoleWidget
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('PyQtGraph Console Dock Example')
# Create the dock area
self.dock_area = DockArea()
self.setCentralWidget(self.dock_area)
# Create and add console dock
self.console_dock = Dock("Console", size=(1, 1))
self.console_widget = ConsoleWidget()
self.console_dock.addWidget(self.console_widget)
self.dock_area.addDock(self.console_dock, 'bottom')
# Print some messages to the console
self.print_to_console("Message 1")
def print_to_console(self, message):
"""Print a message to the console widget"""
self.console_widget.repl.write(message + '\n', style='output')
#self.console_widget.repl.write(message + '\n', style='command')
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec())
Thank you...
Hello @wjassim.
The problem you are experiencing is due to this If-Statement.
if style == 'output' and row == self._lastCommandRow + 1:
# adjust style for first line of output
firstLine, endl, strn = strn.partition('\n')
self._setTextStyle('output_first_line')
self.output.insertPlainText(firstLine + endl)
Within the condition, an attempt is made to add a 1 to the variable self._lastCommandRow
. However, the variable is set to None by default. For this reason, the exception is raised, as None and Int cannot be added together. Only if style == 'command' is passed to the function, the variable self._lastCommandRow
will be overwritten with the value of row.
I have tried to add an additional If statement within the write-method in repl_widget.py.
row = cursor.blockNumber()
if style == 'command':
self._lastCommandRow = row
if self._lastCommandRow is None:
self._lastCommandRow = 0
if style == 'output' and row == self._lastCommandRow + 1:
# adjust style for first line of output
firstLine, endl, strn = strn.partition('\n')
self._setTextStyle('output_first_line')
self.output.insertPlainText(firstLine + endl)
Then it also worked for me with style = 'output'. Please try it out and give feedback on whether you were able to achieve the desired behaviour. I hope that I have been able to help you with this.
Hi @mohass98,
Thank you for your reply.
I have updated these lines in repl_widget.py according to your great note, and the updated code works perfectly. It is now valid for both 'command' and 'output' styles.
I am not sure how to inform the developers of pyqtgraph about this issue so they can modify the code. I can close this issue as the problem has been solved.
Thanks a million!
Best regards.