]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxProcess.py
1 # 11/20/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
8 #----------------------------------------------------------------------
10 class TestPanel(wx
.Panel
):
11 def __init__(self
, parent
, ID
, log
):
12 wx
.Panel
.__init
__(self
, parent
, ID
)
16 self
.Bind(wx
.EVT_IDLE
, self
.OnIdle
)
18 # We can either derive from wxProcess and override OnTerminate
19 # or we can let wxProcess send this window an event that is
20 # caught in the normal way...
21 self
.Bind(wx
.EVT_END_PROCESS
, self
.OnProcessEnded
)
25 prompt
= wx
.StaticText(self
, -1, 'Command line:')
26 self
.cmd
= wx
.TextCtrl(self
, -1, 'python -u data/echo.py')
27 self
.exBtn
= wx
.Button(self
, -1, 'Execute')
29 self
.out
= wx
.TextCtrl(self
, -1, '', style
=wx
.TE_MULTILINE|wx
.TE_READONLY
)
31 self
.inp
= wx
.TextCtrl(self
, -1, '', style
=wx
.TE_PROCESS_ENTER
)
32 self
.sndBtn
= wx
.Button(self
, -1, 'Send')
33 self
.termBtn
= wx
.Button(self
, -1, 'Close Stream')
34 self
.inp
.Enable(False)
35 self
.sndBtn
.Enable(False)
36 self
.termBtn
.Enable(False)
39 self
.Bind(wx
.EVT_BUTTON
, self
.OnExecuteBtn
, self
.exBtn
)
40 self
.Bind(wx
.EVT_BUTTON
, self
.OnSendText
, self
.sndBtn
)
41 self
.Bind(wx
.EVT_BUTTON
, self
.OnCloseStream
, self
.termBtn
)
42 self
.Bind(wx
.EVT_TEXT_ENTER
, self
.OnSendText
, self
.inp
)
46 box1
= wx
.BoxSizer(wx
.HORIZONTAL
)
47 box1
.Add(prompt
, 0, wx
.ALIGN_CENTER
)
48 box1
.Add(self
.cmd
, 1, wx
.ALIGN_CENTER|wx
.LEFT|wx
.RIGHT
, 5)
49 box1
.Add(self
.exBtn
, 0)
51 box2
= wx
.BoxSizer(wx
.HORIZONTAL
)
52 box2
.Add(self
.inp
, 1, wx
.ALIGN_CENTER
)
53 box2
.Add(self
.sndBtn
, 0, wx
.LEFT
, 5)
54 box2
.Add(self
.termBtn
, 0, wx
.LEFT
, 5)
56 sizer
= wx
.BoxSizer(wx
.VERTICAL
)
57 sizer
.Add(box1
, 0, wx
.EXPAND|wx
.ALL
, 10)
58 sizer
.Add(self
.out
, 1, wx
.EXPAND|wx
.ALL
, 10)
59 sizer
.Add(box2
, 0, wx
.EXPAND|wx
.ALL
, 10)
62 self
.SetAutoLayout(True)
66 if self
.process
is not None:
68 self
.process
.CloseOutput()
72 def OnExecuteBtn(self
, evt
):
73 cmd
= self
.cmd
.GetValue()
75 self
.process
= wx
.Process(self
)
76 self
.process
.Redirect();
77 pid
= wx
.Execute(cmd
, wx
.EXEC_ASYNC
, self
.process
)
78 self
.log
.write('OnExecuteBtn: "%s" pid: %s\n' % (cmd
, pid
))
81 self
.sndBtn
.Enable(True)
82 self
.termBtn
.Enable(True)
83 self
.cmd
.Enable(False)
84 self
.exBtn
.Enable(False)
88 def OnSendText(self
, evt
):
89 text
= self
.inp
.GetValue()
91 self
.log
.write('OnSendText: "%s"\n' % text
)
92 self
.process
.GetOutputStream().write(text
+ '\n')
96 def OnCloseStream(self
, evt
):
97 self
.log
.write('OnCloseStream\n')
98 #print "b4 CloseOutput"
99 self
.process
.CloseOutput()
100 #print "after CloseOutput"
102 def OnIdle(self
, evt
):
103 if self
.process
is not None:
104 stream
= self
.process
.GetInputStream()
108 self
.out
.AppendText(text
)
111 def OnProcessEnded(self
, evt
):
112 self
.log
.write('OnProcessEnded, pid:%s, exitCode: %s\n' %
113 (evt
.GetPid(), evt
.GetExitCode()))
115 stream
= self
.process
.GetInputStream()
119 self
.out
.AppendText(text
)
121 self
.process
.Destroy()
123 self
.inp
.Enable(False)
124 self
.sndBtn
.Enable(False)
125 self
.termBtn
.Enable(False)
126 self
.cmd
.Enable(True)
127 self
.exBtn
.Enable(True)
130 #----------------------------------------------------------------------
132 def runTest(frame
, nb
, log
):
133 win
= TestPanel(nb
, -1, log
)
137 #----------------------------------------------------------------------
144 wxProcess lets you get notified when an asyncronous child process
145 started by wxExecute terminates, and also to get input/output streams
146 for the child process's stdout, stderr and stdin.
149 This demo launches a simple python script that echos back on stdout
150 lines that it reads from stdin. You can send text to the echo
151 process' stdin by typing in the lower textctrl and clicking Send.
154 Clicking the Close Stream button will close the demo's end of the
155 stdin pipe to the child process. In our case that will cause the
156 child process to exit its main loop.
162 if __name__
== '__main__':
165 run
.main(['', os
.path
.basename(sys
.argv
[0])])