HarryPython hat geschrieben:Ich möchte doch nur bei meinem Button in der Klasse Main das Label wieder auf "Start" setzen wenn der Tread zuende ist!
Hallo HarryPython!
Nicht so kompliziert, aber mit weniger Möglichkeiten:
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
import wx
import thread
import time
wx.SetDefaultPyEncoding("iso-8859-15")
class MyFrame(wx.Frame):
def __init__(
self, parent = None, id = -1, title = "Example", size = wx.Size(200, 150)
):
wx.Frame.__init__(self, parent, id, title, size = size)
self.panel = wx.Panel(self)
vbox_main = wx.BoxSizer(wx.VERTICAL)
self.panel.SetSizer(vbox_main)
vbox_main.AddStretchSpacer()
self.btn_start = wx.Button(self.panel, label = "Start")
vbox_main.Add(self.btn_start, 0, wx.ALL | wx.ALIGN_CENTER)
self.btn_start.Bind(wx.EVT_BUTTON, self.start_worker)
vbox_main.AddStretchSpacer()
self.panel.Layout()
def start_worker(self, event = None):
self.btn_start.SetLabel("working...")
self.btn_start.Disable()
self.panel.Layout()
wx.SafeYield()
thread.start_new_thread(self.worker, ())
def worker(self):
for i in xrange(5):
print "Ich arbeite..."
time.sleep(1)
# Fertig
wx.CallAfter(self.btn_start.SetLabel, "Start")
wx.CallAfter(self.btn_start.Enable)
wx.CallAfter(self.panel.Layout)
wx.SafeYield()
def main():
"""Testing"""
app = wx.PySimpleApp()
f = MyFrame()
f.Center()
f.Show()
app.MainLoop()
if __name__ == "__main__":
main()
Komplizierter, aber mehr Möglichkeiten:
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
import wx
import time
import threading
wx.SetDefaultPyEncoding("iso-8859-15")
class MyWorker(threading.Thread):
def __init__(self, finished_function = None):
threading.Thread.__init__(self)
self.canceled = threading.Event()
self.finished_function = finished_function
def run(self):
for i in xrange(10):
if self.canceled.isSet():
print "Gestoppt..."
break
print "Ich arbeite..."
time.sleep(1)
if self.finished_function:
self.finished_function()
def stop(self):
self.canceled.set()
class MyFrame(wx.Frame):
def __init__(
self, parent = None, id = -1, title = "Example", size = wx.Size(200, 150)
):
wx.Frame.__init__(self, parent, id, title, size = size)
self.worker = None
self.panel = wx.Panel(self)
vbox_main = wx.BoxSizer(wx.VERTICAL)
self.panel.SetSizer(vbox_main)
vbox_main.AddStretchSpacer()
self.btn_start = wx.Button(self.panel, label = "Start")
vbox_main.Add(self.btn_start, 0, wx.ALL | wx.ALIGN_CENTER)
self.btn_start.Bind(wx.EVT_BUTTON, self.start_worker)
self.btn_cancel = wx.Button(self.panel, label = "Abbruch")
self.btn_cancel.Disable()
vbox_main.Add(self.btn_cancel, 0, wx.ALL | wx.ALIGN_CENTER)
self.btn_cancel.Bind(wx.EVT_BUTTON, self.cancel_worker)
vbox_main.AddStretchSpacer()
self.panel.Layout()
def start_worker(self, event = None):
self.btn_start.SetLabel("working...")
self.btn_start.Disable()
self.btn_cancel.Enable()
self.panel.Layout()
wx.SafeYield()
self.worker = MyWorker(self.worker_finished)
self.worker.start()
def cancel_worker(self, event = None):
self.worker.stop()
def worker_finished(self):
wx.CallAfter(self.btn_start.SetLabel, "Start")
wx.CallAfter(self.btn_start.Enable)
wx.CallAfter(self.btn_cancel.Disable)
wx.CallAfter(self.panel.Layout)
wx.SafeYield()
def main():
"""Testing"""
app = wx.PySimpleApp()
f = MyFrame()
f.Center()
f.Show()
app.MainLoop()
if __name__ == "__main__":
main()
mfg
Gerold
PS: Schon spät... wenig Kommentare!