Hi,
I want to redirect the console or command line output to a python gui in realtime.
But most of the codes i have seen waits for the script to complete than show up in gui.
My script is long and it shows is progress while executing, i want a realtime output on gui.
Code snippet: import wx
from wx.lib.delayedresult import startWorker
from subprocess import Popen, PIPE
class MainWindow(wx.Frame):
def init(self, *args, **kwargs):
wx.Frame.init(self, *args, **kwargs)
self.panel = wx.Panel(self)
self.button = wx.Button(self.panel, label=“Run!”)
self.command = wx.TextCtrl(self.panel)
self.result = wx.TextCtrl(self.panel, style=wx.TE_MULTILINE)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.command, 0, wx.EXPAND)
self.sizer.Add(self.button, 0, wx.EXPAND)
self.sizer.Add(self.result, 1, wx.EXPAND)
self.command.SetValue(“dir”)
self.button.Bind(wx.EVT_BUTTON, self.CallCommand)
self.panel.SetSizerAndFit(self.sizer)
self.Show()
def CallCommand(self, e):
startWorker(self.WorkCommandDone, self.WorkCommand)
def WorkCommand(self):
self.button.Disable()
p = Popen(self.command.GetValue(), shell=True,
stdin=PIPE, stdout=PIPE, stderr=PIPE)
while True:
line = p.stdout.readline()
if line != ‘’:
wx.CallAfter(self.result.AppendText, line)
else:
break
def WorkCommandDone(self, result):
self.button.Enable()
app = wx.App(False)
win = MainWindow(None)
app.MainLoop()
I will highly appreciate if somebody can help me in modifying this, or suggest any better way.
You may need to call wx.Yield() after the wx.CallAfter(). I did something similar to what you’re talking about, but I redirected stdout to a text control. I actually documented it too here:
Perhaps one of the techniques in that article will help you.
Mike
···
On Tuesday, February 24, 2015 at 10:46:10 AM UTC-6, Kamlendra Chandra wrote:
Hi,
I want to redirect the console or command line output to a python gui in realtime.
But most of the codes i have seen waits for the script to complete than show up in gui.
My script is long and it shows is progress while executing, i want a realtime output on gui.
Code snippet: import wx
from wx.lib.delayedresult import startWorker
from subprocess import Popen, PIPE
class MainWindow(wx.Frame):
def init(self, *args, **kwargs):
wx.Frame.init(self, *args, **kwargs)
self.panel = wx.Panel(self)
self.button = wx.Button(self.panel, label=“Run!”)
self.command = wx.TextCtrl(self.panel)
self.result = wx.TextCtrl(self.panel, style=wx.TE_MULTILINE)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.command, 0, wx.EXPAND)
self.sizer.Add(self.button, 0, wx.EXPAND)
self.sizer.Add(self.result, 1, wx.EXPAND)
self.command.SetValue(“dir”)
self.button.Bind(wx.EVT_BUTTON, self.CallCommand)
self.panel.SetSizerAndFit(self.sizer)
self.Show()
def CallCommand(self, e):
startWorker(self.WorkCommandDone, self.WorkCommand)
def WorkCommand(self):
self.button.Disable()
p = Popen(self.command.GetValue(), shell=True,
stdin=PIPE, stdout=PIPE, stderr=PIPE)
while True:
line = p.stdout.readline()
if line != ‘’:
wx.CallAfter(self.result.AppendText, line)
else:
break
def WorkCommandDone(self, result):
self.button.Enable()
app = wx.App(False)
win = MainWindow(None)
app.MainLoop()
I will highly appreciate if somebody can help me in modifying this, or suggest any better way.
In the past on MS-Windows the native textctrl sometimes had a funky glitch when its value hit 64K byte boundries where it would drop a few characters. I think the solution was wx.TE_RICH2
Hi,
I want to redirect the console or command line output to a python gui in realtime.
But most of the codes i have seen waits for the script to complete than show up in gui.
My script is long and it shows is progress while executing, i want a realtime output on gui.
Code snippet: import wx
from wx.lib.delayedresult import startWorker
from subprocess import Popen, PIPE
class MainWindow(wx.Frame):
def init(self, *args, **kwargs):
wx.Frame.init(self, *args, **kwargs)
self.panel = wx.Panel(self)
self.button = wx.Button(self.panel, label=“Run!”)
self.command = wx.TextCtrl(self.panel)
self.result = wx.TextCtrl(self.panel, style=wx.TE_MULTILINE)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.command, 0, wx.EXPAND)
self.sizer.Add(self.button, 0, wx.EXPAND)
self.sizer.Add(self.result, 1, wx.EXPAND)
self.command.SetValue(“dir”)
self.button.Bind(wx.EVT_BUTTON, self.CallCommand)
self.panel.SetSizerAndFit(self.sizer)
self.Show()
def CallCommand(self, e):
startWorker(self.WorkCommandDone, self.WorkCommand)
def WorkCommand(self):
self.button.Disable()
p = Popen(self.command.GetValue(), shell=True,
stdin=PIPE, stdout=PIPE, stderr=PIPE)
while True:
line = p.stdout.readline()
if line != ‘’:
wx.CallAfter(self.result.AppendText, line)
else:
break
def WorkCommandDone(self, result):
self.button.Enable()
app = wx.App(False)
win = MainWindow(None)
app.MainLoop()
I will highly appreciate if somebody can help me in modifying this, or suggest any better way.
Thanks,
Kamlendra
–
You received this message because you are subscribed to the Google Groups “wxPython-users” group.