I write blocking gui program with multiprocessing.
I can’t write text on textctrl and push button while it execute.
This code do “witetext” throgh “queue” with EVT_BUTTON and EVT_IDLE.
I want it to non-blocking program.
I read http://wiki.wxpython.org/Non-Blocking%20Gui, but I can’t modify…
Please advice me.
coding: UTF-8
import wx
import multiprocessing
import time
class MyFrame(wx.Frame):
def init(self, parent, id, title, queue):
wx.Frame.init(self, parent, id, title=title, size=(400, 400))
self.queue = queue
panel = wx.Panel(self, -1)
vbox = wx.BoxSizer(wx.VERTICAL)
hbox1 = wx.BoxSizer(wx.HORIZONTAL)
self.tc1 = wx.TextCtrl(panel, -1)
hbox1.Add(self.tc1, 1)
vbox.Add(hbox1, 0, wx.EXPAND)
hbox2 = wx.BoxSizer(wx.HORIZONTAL)
ok_button = wx.Button(panel, -1, “OK”, size=(70, 30))
cancel_button = wx.Button(panel, -1, “Cancel”, size=(70, 30))
ok_button.Bind(wx.EVT_BUTTON, self.click_okbutton)
cancel_button.Bind(wx.EVT_BUTTON, self.click_cancelbutton)
hbox2.Add(ok_button, 0)
hbox2.Add(cancel_button, 0)
vbox.Add(hbox2, 0, wx.ALIGN_RIGHT)
hbox3 = wx.BoxSizer(wx.HORIZONTAL)
self.tc2 = wx.TextCtrl(panel, -1, style=wx.TE_READONLY)
hbox3.Add(self.tc2, 1, wx.EXPAND)
vbox.Add(hbox3, 1, wx.EXPAND)
panel.SetSizer(vbox)
self.Bind(wx.EVT_CLOSE, self.onClose)
self.Bind (wx.EVT_IDLE, self.onIdle)
self.Show(True)
def click_okbutton(self, event):
self.queue.put(self.tc1.GetValue())
self.tc1.Clear()
def click_cancelbutton(self, event):
self.tc1.Clear()
def onIdle(self, event):
while True:
item = self.queue.get()
if item is None:
print “done!”
break
else:
self.tc2.WriteText(item)
def onClose(self, event):
self.queue.put(None)
self.tc2.WriteText(“closing”)
self.Destroy()
def start_gui(ev, queue):
app = wx.App(False)
frame = MyFrame(None, -1, “sample.py”, queue)
app.MainLoop()
ev.set()
def worker(queue):
while True:
queue.put(“on process\n”)
time.sleep(2)
if name == ‘main’:
multiprocessing.freeze_support()
queue = multiprocessing.Queue()
ev = multiprocessing.Event()
p = multiprocessing.Process(target = start_gui, args=(ev, queue,))
p.start()
p2 = multiprocessing.Process(target = worker, args=(queue,))
p2.start()
ev.wait()
p2.terminate()