Forum: Poser Python Scripting


Subject: A multithreading in Poser Python.

JTrout opened this issue on Nov 02, 2011 · 9 posts


JTrout posted Sun, 06 November 2011 at 8:47 AM

Hi, again

I can't get the expected behavior of callback in Poser.
It seems the event can't get the chance when it is handled.
The following sample code doesn't do an expected behavior with poser.  
The program which has the same event structure do an expected behavior without poser.

What do I mistake?

#-------------------------------------------------------------

-- coding: utf-8 --

import poser
import wx, wx.aui, wx.lib.newevent
import threading, time

(UpdateButtonEvent, EVT_UPDATE_BUTTON) = wx.lib.newevent.NewEvent()
(UpdateCountEvent, EVT_UPDATE_COUNT) = wx.lib.newevent.NewEvent()

class UserEvaluationView(wx.Panel):
    def init(self, model, *args, *kwds):
        super(UserEvaluationView, self).init(args, kwds)
        self.countLabel = wx.StaticText(id=wx.ID_ANY,parent=self, label='', name='count', size = (100,30), style=0)
        self.startButton = wx.Button(self, wx.ID_ANY,"Start", size=(45,20))
        self.startButton.Bind(wx.EVT_BUTTON, self.OnStart)
        self.abortButton = wx.Button(self, wx.ID_ANY,"Abort", size=(45,20))
        self.abortButton.Bind(wx.EVT_BUTTON, self.OnAbort)
        self.abortButton.Disable()
        vLayout1 = wx.BoxSizer(wx.VERTICAL)
        vLayout1.Add(self.countLabel, flag=wx.ALIGN_LEFT|wx.ALL, border=5)
        vLayout1.Add(self.startButton, flag=wx.ALIGN_LEFT|wx.ALL, border=5)
        vLayout1.Add(self.abortButton, flag=wx.ALIGN_LEFT|wx.ALL, border=5)
        self.SetSizerAndFit(vLayout1)
**        self.Bind(EVT_UPDATE_BUTTON, self.OnUpdateButton)

**        self.Bind(EVT_UPDATE_COUNT, self.OnUpdateCount)

        self.thread = None

    def OnStart(self, event):
        if self.thread == None:
            self.thread = TestThread(self)
            self.thread.start()
        
    def OnAbort(self, event):
        if self.thread != None:
            self.thread.abort()
        
    def setForRunning(self):
        self.startButton.Disable()
        self.abortButton.Enable()
        
    def setForStop(self):
        self.startButton.Enable()
        self.abortButton.Disable()
        
    def OnUpdateButton(self, event):
        if event.name == "start":
            self.setForRunning()
        elif event.name== "stop":
            self.setForStop()
        self.Refresh()
        
    def OnUpdateCount(self, event):
        self.countLabel.SetLabel('%3i' % event.count)

class TestThread(threading.Thread):
    def init(self, aView):
        threading.Thread.init(self)
        self.view = aView
        self.setDaemon(True)
        self.abortInvoked = False
        
    def run(self):
**        event = UpdateButtonEvent(name="start")**
**        wx.PostEvent(self.view, event) **
        for i in range(20): # for safety
            if self.abortInvoked:
                break
**            event = UpdateCountEvent(count=i)**
**            wx.PostEvent(self.view, event)**
            # Actually, various processing is put here.
            time.sleep(1) # TBD
**        event = UpdateButtonEvent(name="stop")**
**        wx.PostEvent(self.view, event)**
        self.view.thread = None
            
    def abort(self):
        self.abortInvoked=True

def main():
    model = None # dummy
    man = poser.WxAuiManager()
    root = man.GetManagedWindow()
    panel = UserEvaluationView(model, parent=root)
    pinfo = wx.aui.AuiPaneInfo()pinfo.CloseButton(True).DestroyOnClose(True).Resizable().Float().FloatingSize(wx.Size(200, 200)).BestSize(wx.Size(200, 200))
    man.AddPane(panel, pinfo)
    pinfo.Show()
    man.Update()

main()

#-------------------------------------------------------------