Python Forum
Embed Python console in GUI application
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Embed Python console in GUI application
#2
I made quite a lot of headway. I have a way of entering python code in a GUI control that works much line code.InteractiveConsole and I redirect stdout and stderr so they appear in my GUI:
import sys
import code
import PySide2.QtWidgets as QtWidgets
import PySide2.QtCore as QtCore
import PySide2.QtGui as QtGui
from contextlib import redirect_stdout, redirect_stderr

class Console(QtWidgets.QWidget):
<snip>
    def write(self, line: str):
        self.writeoutput(line, self.outfmt)

    def writeoutput(self, line: str, fmt: QtGui.QTextCharFormat=None) -> None:
        if fmt is not None:
            self.outdisplay.setCurrentCharFormat(fmt)
        self.outdisplay.appendPlainText(line.rstrip())

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    console = Console()
    <snip>
    with redirect_stdout(console), redirect_stderr(console):
        console.show()
        sys.exit(app.exec_())
The program creates a format for characters typed as input (black) and another for captured output (blue). I would like a third format (red) for captured stderr. How do I differentiate stdout from stderr?

One idea I have is creating a redirect class that calls another function when it's write method gets called. I added an errorwrite method to my console class and create a redirect object to call that method. Then I redirect stderr to my redirect object instead of the console

class Redirect():
    def __init__(self, func):
        self.func = func

    def write(self, line:str):
        self.func(line)

class Console(QtWidgets.QWidget):
<snip>
    def errorwrite(self, line: str):
        self.writeoutput(line, self.errfmt)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    console = Console()
    redirect = Redirect(console.errorwrite)
    with redirect_stdout(console), redirect_stderr(redirect):
        console.show()
        sys.exit(app.exec_())
This works, but is there a better way?

Also, at what point is it polite to provide code snippets instead of enough code for a working example. My console.py file is 160 lines long and there isn't much fat to remove and still have it work.
Reply


Messages In This Thread
RE: Embed Python console in GUI application - by deanhystad - Mar-22-2020, 05:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Application development in Python muralikreddy 4 3,089 Mar-27-2024, 12:05 AM
Last Post: anastasiastefanyuk
  How to make cross platform gui python application ? Jayam 2 3,902 Dec-24-2021, 03:24 PM
Last Post: Linenloid
  [PyQt] Embed Google Maps in PyQt6 Widget Raures 2 3,175 Sep-11-2021, 04:32 PM
Last Post: Raures
  [PyGUI] Python Application Not Finding Excel File dseals26 2 2,918 Feb-17-2021, 01:45 AM
Last Post: thewolf
  Python Application in Taskbar constantin01 3 6,078 Jan-24-2020, 10:57 AM
Last Post: buran
  Interacting with python console while program is running Murmele 2 3,380 Feb-10-2018, 05:43 PM
Last Post: kmcollins
  Python Wrapper Application panick1992 8 6,803 Mar-15-2017, 11:54 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020