완전한 작동 예제입니다.
import wx
import functools
import threading
import subprocess
import time
class Frame(wx.Frame):
def __init__(self):
super(Frame, self).__init__(None, -1, 'Threading Example')
# add some buttons and a text control
panel = wx.Panel(self, -1)
sizer = wx.BoxSizer(wx.VERTICAL)
for i in range(3):
name = 'Button %d' % (i+1)
button = wx.Button(panel, -1, name)
func = functools.partial(self.on_button, button=name)
button.Bind(wx.EVT_BUTTON, func)
sizer.Add(button, 0, wx.ALL, 5)
text = wx.TextCtrl(panel, -1, style=wx.TE_MULTILINE|wx.TE_READONLY)
self.text = text
sizer.Add(text, 1, wx.EXPAND|wx.ALL, 5)
panel.SetSizer(sizer)
def on_button(self, event, button):
# create a new thread when a button is pressed
thread = threading.Thread(target=self.run, args=(button,))
thread.setDaemon(True)
thread.start()
def on_text(self, text):
self.text.AppendText(text)
def run(self, button):
cmd = ['ls', '-lta']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in proc.stdout:
wx.CallAfter(self.on_text, line)
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = Frame()
frame.Show()
app.MainLoop()
유용한 예입니다. 비록 제 질문에서 언급 한 Tk 예제와 같지 않습니다. 이 예에서는 외부 명령을 실행하려고하는 요청의 일부를 무시합니다. 예, 당신의 예제에서 추정하는 것이 상당히 쉽지만,이 예제에 걸려 넘어지는 초보자가 알아낼 수 있을지 확신 할 수 없습니다.wxPython의 속도를 빠르게 높이는 것 외에도 질문을 올리려는 나의 희망은 두 언어로 동일한 작은 응용 프로그램을 표시하는 것이 었습니다. 어쨌든 고마워. –
리팩토링하여 하위 프로세스 시작 표시. – FogleBird