[英]Redirect sys.stdout from ping results to a wx.TextCtrl widget
I'm trying to redirect the sys.stdout stream to a wx.TextCtrl widget. 我正在尝试将sys.stdout流重定向到wx.TextCtrl小部件。 In this code, i used ping but how to redirect results to wx.textctrl or statictext widgets.
在这段代码中,我使用了ping命令,但是如何将结果重定向到wx.textctrl或statictext小部件。 I can send variable from my input to wx.statictext but not able to get the stdout from ping results.
我可以将输入中的变量发送到wx.statictext,但不能从ping结果中获取标准输出。 hope someone here can help.
希望这里有人能帮忙。
I'm using python 3.6.0 with wx 2.8.12.1 我正在将python 3.6.0和wx 2.8.12.1一起使用
import wx
import os
class RandomPanel(wx.Panel):
def __init__(self, parent, color):
wx.Panel.__init__(self, parent)
self.SetBackgroundColour(color)
class MainPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
topSplitter = wx.SplitterWindow(self)
vSplitter = wx.SplitterWindow(topSplitter)
panelOne = RandomPanel(vSplitter, "white")
self.txt = wx.TextCtrl(panelOne,
style=wx.TE_PROCESS_ENTER,
pos=(7, 8), size=(330, 30))
self.txt.SetFocus()
self.txt.Bind(wx.EVT_TEXT_ENTER, self.ping)
panelTwo = RandomPanel(vSplitter, "white")
vSplitter.SplitVertically(panelOne, panelTwo)
vSplitter.SetSashGravity(0.5)
panelThree = RandomPanel(topSplitter, "black")
topSplitter.SplitHorizontally(vSplitter, panelThree)
topSplitter.SetSashGravity(0.5)
self.term = wx.StaticText(panelThree, -1, '', pos=(6, 100))
self.term.SetForegroundColour((255, 255, 255)) #set font color
self.term.SetBackgroundColour((0, 0, 0)) #set background color
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(topSplitter, 1, wx.EXPAND)
self.SetSizer(sizer)
def ping(self, event):
put = self.txt.GetValue()
self.p = os.system("ping -n 1 "+put)
self.term.SetLabel(put)
self.p.AppendText('>>> ')
self.p.AppendText(event)
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="ping tool",
size=(800, 600))
panel = MainPanel(self)
self.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
app.MainLoop()
Replace import os
with from subprocess import call, Popen, PIPE, STDOUT
Then replace 用
from subprocess import call, Popen, PIPE, STDOUT
替换import os
from subprocess import call, Popen, PIPE, STDOUT
然后替换
self.p = os.system("ping -n 1 "+put)
self.term.SetLabel(put)
self.p.AppendText('>>> ')
self.p.AppendText(event)
with 与
comm = Popen(['ping',put],stdout=PIPE,stderr=STDOUT,universal_newlines=True)
for i in iter(comm.stdout.readline, b''):
if i != '': pass
else: break
self.term.SetLabel(str(i))
wx.Yield()
This will read the output from the ping command, output it and then call Yield
to allow the wx.App() mainloop to update the screen from within the for
loop 这将从ping命令读取输出,将其输出,然后调用
Yield
允许wx.App()主循环从for
循环内更新屏幕
You will probably want to put a stop mechanism in there at some point. 您可能会希望在某个位置放置一个停止机制。
Edit: To create a set of rolling results, I would change self.term
from a StaticText
to a TextCtrl
编辑:要创建一组滚动结果,我将
self.term
从StaticText
更改为TextCtrl
self.term = wx.TextCtrl(panelThree, -1, '',size=(400,200),style=wx.TE_MULTILINE)
and then in the ping
routine change SetLabel
to AppendText
然后在
ping
例程SetLabel
AppendText
更改为AppendText
def ping(self, event):
put = self.txt.GetValue()
comm = Popen(['ping',put],stdout=PIPE,stderr=STDOUT,universal_newlines=True)
for i in iter(comm.stdout.readline, b''):
if i != '': pass
else: break
self.term.AppendText(str(i))
wx.Yield()
@Rolf of Saxony, i have updated the code thanks for your help. @Rox of Saxony,我已更新代码,感谢您的帮助。
def ping(self, event):
a = []
put = self.txt.GetValue()
comm = Popen(['ping', put], stdout=PIPE, stderr=STDOUT, universal_newlines=True)
for i in iter(comm.stdout.readline, b''):
if i != '':
pass
else:
break
a.append(str(i))
self.term.SetLabelText("start pinging......")
self.term.SetLabelText("\n".join(a))
wx.Yield()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.