Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@raphigaziano
Last active August 16, 2021 15:12
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save raphigaziano/4494398 to your computer and use it in GitHub Desktop.
Save raphigaziano/4494398 to your computer and use it in GitHub Desktop.
A simple PyQt output widget which can be used as basic debug console
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
qtdbg.py
A simple PyQt output widget.
It's main use case is to serve as an output console, for debugging or
other purposes.
It provides a file-like interface for ease of integration with other
python features such as the logging module, on top of a slightly
pre-set QTextEdit widget.
Since it inherits QTextEdit directly, all of the widget's methods are
available directly for further customization or GUI integration.
Tested on:
- Python 3.2, PyQt4, win7
Author: raphi <r.gaziano@gmail.com>
Created: 08/01/2013
Version: 1.0
>>> import sys
>>> app = QtGui.QApplication(sys.argv)
>>> widget = QDbgConsole()
>>> widget.write("TestMessage, yay \o/")
>>> widget.seek(0)
0
>>> widget.read()
'TestMessage, yay \\\o/'
>>> widget.seek(0)
0
>>> s = widget.read(4)
>>> assert(len(s) == 4)
>>> print(s)
Test
"""
from io import StringIO
from PyQt4 import QtGui
class QDbgConsole(QtGui.QTextEdit):
'''
A simple QTextEdit, with a few pre-set attributes and a file-like
interface.
'''
# Feel free to adjust those
WIDTH = 480
HEIGHT = 320
def __init__(self, parent=None, w=WIDTH, h=HEIGHT):
super(QDbgConsole, self).__init__(parent)
self._buffer = StringIO()
self.resize(w, h)
self.setReadOnly(True)
### File-like interface ###
###########################
def write(self, msg):
'''Add msg to the console's output, on a new line.'''
self.insertPlainText(msg)
# Autoscroll
self.moveCursor(QtGui.QTextCursor.End)
self._buffer.write(msg)
# Most of the file API is provided by the contained StringIO
# buffer.
# You can redefine any of those methods here if needed.
def __getattr__(self, attr):
'''
Fall back to the buffer object if an attribute can't be found.
'''
return getattr(self._buffer, attr)
# -- Testing
if __name__ == '__main__':
import doctest
doctest.testmod(verbose=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment