<div dir="ltr">Ciao a tutti,<div><br></div><div>sto cercando di capire come gestire un thread avviandolo con wxpython.</div><div>Ho trovato questo:</div><div><br></div><div><div>import time</div><div>import wx</div><div> </div><div>from threading import Thread</div><div> </div><div># Define notification event for thread completion</div><div>EVT_RESULT_ID = wx.NewId()</div><div> </div><div>def EVT_RESULT(win, func):</div><div> """Define Result Event."""</div><div> win.Connect(-1, -1, EVT_RESULT_ID, func)</div><div> </div><div>class ResultEvent(wx.PyEvent):</div><div> """Simple event to carry arbitrary result data."""</div><div> def __init__(self, data):</div><div> """Init Result Event."""</div><div> wx.PyEvent.__init__(self)</div><div> self.SetEventType(EVT_RESULT_ID)</div><div> self.data = data</div><div> </div><div>########################################################################</div><div>class TestThread(Thread):</div><div> """Test Worker Thread Class."""</div><div> </div><div> #----------------------------------------------------------------------</div><div> def __init__(self, wxObject):</div><div> """Init Worker Thread Class."""</div><div> Thread.__init__(self)</div><div> self.wxObject = wxObject</div><div> self.start() # start the thread</div><div> </div><div> #----------------------------------------------------------------------</div><div> def run(self):</div><div> """Run Worker Thread."""</div><div> # This is the code executing in the new thread.</div><div> for i in range(6):</div><div> time.sleep(10)</div><div> amtOfTime = (i + 1) * 10</div><div> wx.PostEvent(self.wxObject, ResultEvent(amtOfTime))</div><div> time.sleep(5)</div><div> wx.PostEvent(self.wxObject, ResultEvent("Thread finished!"))</div><div> </div><div>########################################################################</div><div>class MyForm(wx.Frame):</div><div> </div><div> #----------------------------------------------------------------------</div><div> def __init__(self):</div><div> wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")</div><div> </div><div> # Add a panel so it looks the correct on all platforms</div><div> panel = wx.Panel(self, wx.ID_ANY)</div><div> self.displayLbl = wx.StaticText(panel, label="Amount of time since thread started goes here")</div><div> self.btn = btn = wx.Button(panel, label="Start Thread")</div><div> </div><div> btn.Bind(wx.EVT_BUTTON, self.onButton)</div><div> </div><div> sizer = wx.BoxSizer(wx.VERTICAL)</div><div> sizer.Add(self.displayLbl, 0, wx.ALL|wx.CENTER, 5)</div><div> sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)</div><div> panel.SetSizer(sizer)</div><div> </div><div> # Set up event handler for any worker thread results</div><div> EVT_RESULT(self, self.updateDisplay)</div><div> </div><div> #----------------------------------------------------------------------</div><div> def onButton(self, event):</div><div> """</div><div> Runs the thread</div><div> """</div><div> TestThread(self)</div><div> self.displayLbl.SetLabel("Thread started!")</div><div> btn = event.GetEventObject()</div><div> btn.Disable()</div><div> </div><div> #----------------------------------------------------------------------</div><div> def updateDisplay(self, msg):</div><div> """</div><div> Receives data from thread and updates the display</div><div> """</div><div> t = msg.data</div><div> if isinstance(t, int):</div><div> self.displayLbl.SetLabel("Time since thread started: %s seconds" % t)</div><div> else:</div><div> self.displayLbl.SetLabel("%s" % t)</div><div> self.btn.Enable()</div><div> </div><div>#----------------------------------------------------------------------</div><div># Run the program</div><div>if __name__ == "__main__":</div><div> app = wx.PySimpleApp()</div><div> frame = MyForm().Show()</div><div> app.MainLoop()</div><div><br></div><div>Pare funzionare, anche se ci sono cose che per me sono misteriose...</div><div><br></div><div>Tuttavia, come faccio a terminare il thread avviato una volta che clicco la x di chiusura o che metto lì un pulsante "chiudi"?</div><div><br></div><div>Tenete presente che il tutto è sotto windows.</div><div><br></div><div>Quello che succede (anche se non sempre) è che chiudendo, il programma va in crash probabilmente proprio per il fatto che il thread rimane vivo e tenta di restituire qualcosa che non è più possibile restituire.</div><div><br></div><div>Grazie a tutti. </div><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><font face="monospace">Riccardo Brazzale</font></div><div dir="ltr"><br></div></div></div></div></div>
</div></div>