würde zwar zum anderen Thema ("Probleme mit Threads") passen,
aber der Übersichtlichkeit starte ich einen neuen Thread über threads.
Verwirrend?

Könnte zwar auch zu den allgemeinen Fragen passen,
aber da hier soviele wx Sachen verwendet werden,
poste ich lieber hier.
Mit Threads kenne ich mich nicht so aus.
Ich habe das vorige Sample weitergestrickt.
Was mir nicht ganz klar ist:
1) Es gibt zwei Module (thread und threading).
Sind das manchmal doppelte Methoden (kann man von thread oder threading nehmen) oder hat jedes seine Berechtigung.
2) Es wird unten
Code: Alles auswählen
while not parent.confirmed:
pass
Leistung in Anspruch nimmt.
Robin Dunn meinte:
A better approach is to make the thread wait with a
threading.Event or similar.
If you have a threading.Event instance then you can make some
thread(s) to wait until the "event" happens with event.wait() and then
the thread(s) will wait until some other thread wants to signal the
event and calls event.set().
Das ist mir leider ein wenig zu hoch, bzw. habe mich mit Threads
wie gesagt, noch zu wenig auseinandergesetzt.
Wer kann mir helfen, das in das Programm einzubauen?

Danke im voraus!
Code: Alles auswählen
import wx
import time
from thread import start_new
EVT_RESULT_ID = wx.NewId()
def EVT_RESULT(win, func):
win.Connect(-1, -1, EVT_RESULT_ID, func)
class ResultEvent(wx.PyEvent):
def __init__(self, data):
wx.PyEvent.__init__(self)
self.SetEventType(EVT_RESULT_ID)
self.data = data
def working(parent):
wx.PostEvent(parent, ResultEvent(None))
time.sleep(2)
parent.confirmed = False
wx.PostEvent(parent, ResultEvent(1))
#waiting for user to confirm
while not parent.confirmed:
pass
time.sleep(0.5)
wx.CallAfter (parent.ShowMsg)
class MyApp(wx.App):
def OnInit(self):
frame = Myframe(None)
self.SetTopWindow(frame)
frame.CenterOnScreen()
frame.Show(True)
return True
class Myframe(wx.Frame):
def __init__(self,parent):
wx.Frame.__init__(self, parent, -1, 'Hello', size=(100,100))
btn = wx.Button(self, -1, 'Click here', (10,10), (80,80))
self.Bind(wx.EVT_BUTTON, lambda handler: start_new(working,(self,)), btn)
EVT_RESULT(self,self.OnResult)
self.busy = None
self.confirmed = False
def OnResult(self, event):
if event.data is None:
#self.busy = wx.BusyInfo("working in Background...")
self.busy = wx.Dialog(self, title="Busy", pos=(300,300), size=(100,100))
self.busy.Show(True)
else:
self.busy.Destroy()
wx.MessageBox("Info", "Please Confirm!")
self.confirmed = True
def ShowMsg(self):
wx.MessageBox("Info", "Finished")
x = MyApp(0)
x.MainLoop()