]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/wxProcess.py
wxMGL update
[wxWidgets.git] / wxPython / demo / wxProcess.py
CommitLineData
c368d904
RD
1
2from wxPython.wx import *
3
4#----------------------------------------------------------------------
5
6class TestPanel(wxPanel):
7 def __init__(self, parent, ID, log):
8 wxPanel.__init__(self, parent, ID)
9 self.log = log
10
11 self.process = None
12 EVT_IDLE(self, self.OnIdle)
13
14 # We can either derive from wxProcess and override OnTerminate
15 # or we can let wxProcess send this window an event that is
16 # caught in the normal way...
17 EVT_END_PROCESS(self, -1, self.OnProcessEnded)
18
19
20 # Make the controls
21 prompt = wxStaticText(self, -1, 'Command line:')
22 self.cmd = wxTextCtrl(self, -1, 'python data/echo.py')
23 self.exBtn = wxButton(self, -1, 'Execute')
24
25 self.out = wxTextCtrl(self, -1, '', style=wxTE_MULTILINE|wxTE_READONLY)
26
27 self.inp = wxTextCtrl(self, -1, '', style=wxTE_PROCESS_ENTER)
28 self.sndBtn = wxButton(self, -1, 'Send')
29 self.termBtn = wxButton(self, -1, 'Close Stream')
30 self.inp.Enable(false)
31 self.sndBtn.Enable(false)
32 self.termBtn.Enable(false)
33
34 # Hook up the events
35 EVT_BUTTON(self, self.exBtn.GetId(), self.OnExecuteBtn)
36 EVT_BUTTON(self, self.sndBtn.GetId(), self.OnSendText)
37 EVT_BUTTON(self, self.termBtn.GetId(), self.OnCloseStream)
38 EVT_TEXT_ENTER(self, self.inp.GetId(), self.OnSendText)
39
40
41 # Do the layout
42 box1 = wxBoxSizer(wxHORIZONTAL)
43 box1.Add(prompt, 0, wxALIGN_CENTER)
44 box1.Add(self.cmd, 1, wxALIGN_CENTER|wxLEFT|wxRIGHT, 5)
45 box1.Add(self.exBtn, 0)
46
47 box2 = wxBoxSizer(wxHORIZONTAL)
48 box2.Add(self.inp, 1, wxALIGN_CENTER)
49 box2.Add(self.sndBtn, 0, wxLEFT, 5)
50 box2.Add(self.termBtn, 0, wxLEFT, 5)
51
52 sizer = wxBoxSizer(wxVERTICAL)
53 sizer.Add(box1, 0, wxEXPAND|wxALL, 10)
54 sizer.Add(self.out, 1, wxEXPAND|wxALL, 10)
55 sizer.Add(box2, 0, wxEXPAND|wxALL, 10)
56
57 self.SetSizer(sizer)
58 self.SetAutoLayout(true)
59
60
61 def __del__(self):
62 if self.process is not None:
63 self.process.Detach()
64 self.process.CloseOutput()
65 self.process = None
66
67
68 def OnExecuteBtn(self, evt):
69 cmd = self.cmd.GetValue()
70
71 self.process = wxProcess(self)
72 self.process.Redirect();
73 pid = wxExecute(cmd, false, self.process)
74 self.log.write('OnExecuteBtn: "%s" pid: %s\n' % (cmd, pid))
75
76 self.inp.Enable(true)
77 self.sndBtn.Enable(true)
78 self.termBtn.Enable(true)
79 self.cmd.Enable(false)
80 self.exBtn.Enable(false)
81 self.inp.SetFocus()
82
83
84 def OnSendText(self, evt):
85 text = self.inp.GetValue()
86 self.inp.SetValue('')
87 self.log.write('OnSendText: "%s"\n' % text)
88 self.process.GetOutputStream().write(text + '\n')
89 self.inp.SetFocus()
90
91
92 def OnCloseStream(self, evt):
93 self.log.write('OnCloseStream\n')
94 self.process.CloseOutput()
95
96
97 def OnIdle(self, evt):
98 if self.process is not None:
99 stream = self.process.GetInputStream()
100
101 # Yes, this is weird. For this particular stream, EOF
102 # simply means that there is no data available to be read,
103 # not truly the end of file. Also, read() just reads all
104 # the currently available data, not until the real EOF...
105 if not stream.eof():
106 text = stream.read()
107 self.out.AppendText(text)
108
109
110 def OnProcessEnded(self, evt):
111 self.log.write('OnProcessEnded, pid:%s, exitCode: %s\n' %
112 (evt.GetPid(), evt.GetExitCode()))
113
114 stream = self.process.GetInputStream()
115 if not stream.eof():
116 text = stream.read()
117 self.out.AppendText(text)
118
119 self.process.Destroy()
120 self.process = None
121 self.inp.Enable(false)
122 self.sndBtn.Enable(false)
123 self.termBtn.Enable(false)
124 self.cmd.Enable(true)
125 self.exBtn.Enable(true)
126
127
128#----------------------------------------------------------------------
129
130def runTest(frame, nb, log):
131 win = TestPanel(nb, -1, log)
132 return win
133
134
135#----------------------------------------------------------------------
136
137
138overview = """\
139<html><body>
140<h2>wxProcess</h2>
141
142wxProcess lets you get notified when an asyncronous child process
143started by wxExecute terminates, and also to get input/output streams
144for the child process's stdout, stderr and stdin.
145
146<p>
147This demo launches a simple python script that echos back on stdout
148lines that it reads from stdin. You can send text to the echo
149process' stdin by typing in the lower textctrl and clicking Send.
150
151<p>
152Clicking the Close Stream button will close the demo's end of the
153stdin pipe to the child process. In our case that will cause the
154child process to exit its main loop.
155
156</body><html>
157"""