1

Currently I am able to display my python console output in wxPython GUI. Below is the code for it: -

class RedirectText(object):
            def __init__(self,aWxTextCtrl):
                self.out = aWxTextCtrl

            def write(self,string):
                self.out.WriteText(string)

        self.console = wx.TextCtrl(self.window_2_pane_2, wx.ID_ANY, "Low Power Demo Suite [Version Beta]\nC:\Software\Python\Visualizer> Running Simulation...", style=wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
        self.console.SetMinSize((600, 200))
        self.console.SetBackgroundColour(wx.BLACK)
        self.console.SetForegroundColour(wx.WHITE)
        redir = RedirectText(self.console)
        sys.stdout = redir

The code is working perfectly fine. However what I want is, I want to display the console output in WxPython upto a specific line only.

How can I do the same?

Thanks Regards, Tanmay

10may
  • 311
  • 1
  • 2
  • 10

1 Answers1

0

As you are redirecting sys.stdout the easiest way would be to cancel that, based on a test on the console output or a line count, or whatever your criteria is.

Before you reassign sys.stdout to redir save the original setting with:

oldstdout = sys.stdout

Then whenever your criteria is hit, reapply it with:

sys.stdout = oldstdout
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
  • Hey, How do I check for a specific string, so that I can stop the redirection of std.out when that is encountered on the python console? – 10may Aug 26 '22 at 12:27
  • @TANMAYAGRAWAL You haven't given us a working example, just a code snippet, so I'm not 100% sure but in your snippet, `string`, one assumes, contains the actual text that is being written. – Rolf of Saxony Aug 26 '22 at 18:06
  • @TANMAYAGRAWAL p.s. remember to test against `string.strip()` if you are looking for something specific, because `string` will contain newline characters. – Rolf of Saxony Aug 27 '22 at 07:29
  • Hi @Rolf of Saxony this is the entire working code for having a console embedded in wxPython. Even I'm not sure what string is, this is the code I got while searching the internet. But it doesn't solve my problem completely. – 10may Aug 31 '22 at 12:17
  • Sorry, but I still couldn't understand how are you recommending to stop writing to the embedded console once I encounter a specific string. Can you please elaborate more? – 10may Aug 31 '22 at 12:19